1、创建BgROW类
public class BgROW : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.ToString()=="true")
{
return new SolidColorBrush(Color.FromRgb(46, 170, 231));
}
else
return new SolidColorBrush(Color.FromRgb(229, 73, 54));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}2、在xaml中引入BgRow,然后在<ResourceDictionary>中加入<Bg:BgROW x:Key="bgRow"/>,最后添加<Setter Property="Background" Value="{Binding IsErr, Converter={StaticResource bgRow}}"/>对行背景色进行绑定
<DataGrid x:Name="dgProduct" Grid.Row="1" Margin="50,30,50,30" Background="#FFFFFF" HorizontalGridLinesBrush="Gray" VerticalGridLinesBrush="Transparent" HeadersVisibility="Column" CanUserAddRows="False" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Resources>
<ResourceDictionary>
<Bg:BgROW x:Key="bgRow"/>
<Style x:Key="dgStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="35"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</ResourceDictionary>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="商品编码" Width="1.5*" ElementStyle="{StaticResource dgStyle}" Binding="{Binding Code}"/>
<DataGridTextColumn Header="商品名称" Width="1.5*" ElementStyle="{StaticResource dgStyle}" Binding="{Binding Name}"/>
<DataGridTextColumn Header="款号" Width="1.5*" ElementStyle="{StaticResource dgStyle}" Binding="{Binding Style}"/>
<DataGridTextColumn Header="颜色" Width="*" ElementStyle="{StaticResource dgStyle}" Binding="{Binding Color}"/>
<DataGridTextColumn Header="尺码" Width="*" ElementStyle="{StaticResource dgStyle}" Binding="{Binding Size}"/>
<DataGridTextColumn Header="数量" Width="*" ElementStyle="{StaticResource dgStyle}" Binding="{Binding Count}"/>
<DataGridTextColumn Header="状态" Binding="{Binding Statu}" Visibility="Hidden"/>
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="#D7D7D7"/>
<Setter Property="Height" Value="70"/>
<Setter Property="FontSize" Value="45"/>
<Setter Property="BorderBrush" Value="Gray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="#FF363434"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<!--单元格不可选中-->
<Setter Property="IsHitTestVisible" Value="False"/>
<Setter Property="Height" Value="90"/>
<Setter Property="Background" Value="{Binding IsErr, Converter={StaticResource bgRow}}"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>版权声明:本文为manlibj原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。