怎样通过判断一个行内的字段的值来改变该行的颜色呢?
我们使用绑定来做:
GridControl控件的行的概念很模糊,我们要想动态改变行的颜色不是设置一下Background就能做到的,我们需要改变它的RowStyle,下面是代码:
1 <Style x:Key = "RowStyle" BasedOn = "{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}" TargetType = "{x:Type dxg:GridRowContent}"> 2 <Setter Property = "Background" Value="{Binding Row, Converter={StaticResource alarmLevelConverter}}"/> 3 </Style>
把它赋给
1 <dxg:GridControl.View> 2 <dxg:TableView RowStyle="{DynamicResource RowStyle}" /> 3 </dxg:GridControl.View>
然后转换器怎么写呢?
1 [ValueConversion(typeof(AlarmHappeningClass), typeof(Brush))] 2 3 public class AlarmLevelConverter : IValueConverter 4 { 5 6 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 7 { 8 AlarmHappeningClass alarmrow = value as AlarmHappeningClass; 9 if (alarmrow == null) 10 return Brushes.Transparent; ; 11 if (alarmrow.AlarmLevel=="紧急") 12 return Brushes.Red; 13 else if (alarmrow.AlarmLevel == "重要") 14 return Brushes.Orange; 15 return Brushes.Transparent; 16 } 17 18 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 19 { 20 throw new NotSupportedException(); 21 } 22 23 }
上面的转换器,根据自己的需要改代码就行了,如果在加trigger,自己在代码里面加就可以了。