應用場景:當我們使用devexpress gridcontrol wpf控件時。可要會要根據這一行要顯示的值來設置相應的顏色
可以通過下面方法來實現
一、先定義一個style
<local:Conv x:Key="c"/>
<Style x:Key="optimizedRowStyle" TargetType="{x:Type dxg:RowControl}">
<Setter Property="Background" Value="{Binding Row.Column2, Converter={StaticResource c}}"/>
</Style>
x:Type dxg:RowControl 說明這個style是針對行的
Binding Row.Column2 表示改背景顏色會根據第二列的值來動態設置其顏色
二、把style應用到控件上
<dxg:GridControl ItemsSource="{StaticResource Data}" VerticalAlignment="Stretch" Height="400">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Column1"></dxg:GridColumn>
<dxg:GridColumn FieldName="Column2"></dxg:GridColumn>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True" AllowEditing="False" RowStyle="{StaticResource optimizedRowStyle}" NavigationStyle="Row">
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
是通過RowStyle依賴屬性來設置
三、效果如下:
會根據第二列的值來顯示不同的背景顏色
ps 下面把后台convert類貼出來大家看看
public class Conv : IValueConverter {
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if (!(value is int)) return new SolidColorBrush(Colors.White);
if ((int)value % 3 == 0) return new SolidColorBrush(Colors.Red);
return new SolidColorBrush(Colors.White);
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new System.NotImplementedException();
}
}
這個value就是column2列里面的值。