怎樣通過判斷一個行內的字段的值來改變該行的顏色呢?
我們使用綁定來做:
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,自己在代碼里面加就可以了。