为WPF中textblock的部分文本应用属性
我想,当textblock的一部分文本是“Thomas”时,文本周围是蓝色的 我该怎么做 您需要一个转换器:为WPF中textblock的部分文本应用属性,wpf,textblock,Wpf,Textblock,我想,当textblock的一部分文本是“Thomas”时,文本周围是蓝色的 我该怎么做 您需要一个转换器: public class StringPropertyContainsThomasConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(value != null)
public class StringPropertyContainsThomasConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if(value != null) {
if(value.ToString().Contains("Thomas")) return Brushes.Blue; //replace with whatever color you want
}
return Brushes.White;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
XAML中的用法:
<Window.Resources>
<local:StringPropertyContainsThomasConverter x:Key="StringPropertyContainsThomasConverter"/>
</Window.Resources>
<TextBlock Background="{Binding RelativeSource={RelativeSource self},
Path=Text,
Converter={StaticResources StringPropertyContainsThomasConverter}}"/>