我有一个绑定的组合框,根据我在记录中返回的值,我想更改组合框中某些行的背景颜色。这有可能吗?
多一点澄清。我正在查看每行中的一个字段,并根据其值更改背景颜色。因此,我可以更改所有行,某些行或不更改任何行。
谢谢
使用ItemContainerStyle
设置每行的项目背景色。您可以绑定到行数据上下文中的属性,并使用IValueConverter
来获取适当的画笔。例如,假设项目具有属性“ Y”:
<ComboBox>
<ComboBox.Resources>
<local:BoolToBrushConverter x:Key="BoolToBrushConverter" />
</ComboBox.Resources>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Background"
Value="{Binding Y,Converter={StaticResource BoolToBrushConverter}}" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
然后,“ BoolToBrushConverter”将如下所示:
public class BoolToBrushConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value as bool? == true) ? Brushes.Green : Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句