我想,当文本块的一部分文字是“托马斯”时。在文本周围是蓝色的。
我该怎么做?
答案 0 :(得分:-1)
你需要一个转换器:
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}}"/>