バインドする文字列をConverterで変換する。
どうもこんにちは。
本日はバインドしてある文字列をConverterで変換してみます。
やることは、Converterを用意し、
それをxamlのリソースで定義しておいて、バインド時に設定するだけです。
ConverterはIValueConveterを実装します。ここでは!!をつけるというもの。
class TextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { return (value as String) + "!!"; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } }
xaml側ではリソースで定義しておいて、バインドのところでコンバータをかませます。
<Page.Resources> <local:Moo x:Name="moo"/> <local:TextConverter x:Name="textConverter"/> </Page.Resources> <Grid> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{StaticResource moo}" Text="{Binding Txt, Converter={StaticResource textConverter}}" />
簡単です!