DataGrid中要在原有的样式上添加样式,可以使用BasedOn来设定基础样式,如下面的
BasedOn="{StaticResource DataGridRowStyle}"
前端的基础框架HandyControl,在开发中想设置DataGrid的行的样式,根据某个绑定的值来设定行的背景色(通过转化器设定Convert),代码如下
<UserControl.Resources>
<ResourceDictionary>
<convert:JudgeResultFGConvert x:Key="judgeResultFGConvert" />
</ResourceDictionary>
</UserControl.Resources>
...........
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" BasedOn="{StaticResource DataGridRowStyle}">
<Setter Property="Background" Value="{Binding Path=Panduanjieguo, Converter={StaticResource judgeResultFGConvert}}"/>
</Style>
</DataGrid.RowStyle>
代码解释:Convert是设置好的转化器,转化器的代码如下
[ValueConversion(typeof(int), typeof(Brushes))]
public class JudgeResultFGConvert : IValueConverter
{
//实现接口的两个方法
#region IValueConverter 成员
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Brushes judgeResultFG = null;
if (value != null)
{
int judgeResultInt = (int)value;
if (judgeResultInt == 1)
return new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CCFFE6"));
else if (judgeResultInt == 2)
return new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E0FFFF"));
else if (judgeResultInt == 3)
return new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF0F5"));
}
{
return Brushes.White;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}