引用网址:https://blog.csdn.net/qq_29844879/article/details/80200568
我这里是使用的转换器,直接上代码(部分代码)。
后台代码:
namespace MyControlLibrarys
{
/ 定义转换器
[ValueConversion(typeof(string), typeof(SolidColorBrush))]
public class DataColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || value.ToString() == "")
return "";
if (value.ToString() == "不通过")//这里根据你里面的值自己写判断条件
{
try
{
return new SolidColorBrush(Colors.Red);
}
catch
{ throw; }
}
return new SolidColorBrush(Colors.Black);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
xmal:
xmlns:local="clr-namespace:MyControlLibrarys"
//包含资源
<UserControl.Resources>
<ResourceDictionary>
<local:DataColorConverter x:Key="DataColorConverterStyle"/>
</ResourceDictionary>
</UserControl.Resources>
<DataGrid Grid.Row="1" Grid.Column="0" Name="dgData" AutoGenerateColumns="False" CanUserAddRows="False" GridLinesVisibility="All">
<DataGrid.Columns>
<DataGridTextColumn Width="0.8*" Header="序号" ElementStyle="{StaticResource contentCenterStyle}" Binding="{Binding Path=OrderId, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="测试步骤" Width="2*" Binding="{Binding Path=step, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="测试内容" Width="2*" Binding="{Binding Path=content, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="标准" Width="2*" Binding="{Binding Path=standard, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="实测" Width="2*" Binding="{Binding Path=actual, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<!--DataGridTextColumn Header="结论" Width="2*" Binding="{Binding Path=conclusion, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Foreground="{Binding Path=conclusion, Converter={StaticResource DataColorConverterStyle}}"></-->
<!--这里一定要用下面这种格式才能成功-->
<DataGridTemplateColumn Width="2*" Header="结论">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=conclusion,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
Foreground="{Binding Path=conclusion,Converter={StaticResource DataColorConverterStyle}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
效果:
————————————————
版权声明:本文为CSDN博主「creay_king」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29844879/article/details/80200568