如何使用数据绑定基于属性值设置 DataGrid 的行背景

在我的 XAML 代码中,我想根据一个特定行中对象的值设置每一行的 Background颜色。我有一个 ObservableCollectionz,并且每个 z都有一个称为 State的属性。在我的 DataGrid课程中,我是这样开始的:

<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background"
Value="{Binding z.StateId, Converter={StaticResource StateIdToColorConverter}}"/>
</Style>
</DataGrid.RowStyle>

这是一种错误的方法,因为 x 不是我的 ViewModel 类中的属性。

在我的 ViewModel 类中,我有一个 ObservableCollection<z>,它是这个 DataGridItemsSource,还有一个类型为 zSelectedItem

我可以将颜色绑定到 SelectedItem,但是这只会改变 DataGrid中的一行。

如何基于一个属性更改此行的背景色?

123323 次浏览

Use a DataTrigger:

<DataGrid ItemsSource="{Binding YourItemsSource}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="State1">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="State2">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>

The same can be done without DataTrigger too:

 <DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" >
<Setter.Value>
<Binding Path="State" Converter="{StaticResource BooleanToBrushConverter}">
<Binding.ConverterParameter>
<x:Array Type="SolidColorBrush">
<SolidColorBrush Color="{StaticResource RedColor}"/>
<SolidColorBrush Color="{StaticResource TransparentColor}"/>
</x:Array>
</Binding.ConverterParameter>
</Binding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>

Where BooleanToBrushConverter is the following class:

public class BooleanToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Brushes.Transparent;


Brush[] brushes = parameter as Brush[];
if (brushes == null)
return Brushes.Transparent;


bool isTrue;
bool.TryParse(value.ToString(), out isTrue);


if (isTrue)
{
var brush =  (SolidColorBrush)brushes[0];
return brush ?? Brushes.Transparent;
}
else
{
var brush = (SolidColorBrush)brushes[1];
return brush ?? Brushes.Transparent;
}
}


public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

In XAML, add and define a RowStyle Property for the DataGrid with a goal to set the Background of the Row, to the Color defined in my Employee Object.

<DataGrid AutoGenerateColumns="False" ItemsSource="EmployeeList">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding ColorSet}"/>
</Style>
</DataGrid.RowStyle>

And in my Employee Class

public class Employee {


public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }


public string ColorSet { get; set; }


public Employee() { }


public Employee(int id, string name, int age)
{
Id = id;
Name = name;
Age = age;
if (Age > 50)
{
ColorSet = "Green";
}
else if (Age > 100)
{
ColorSet = "Red";
}
else
{
ColorSet = "White";
}
}
}

This way every Row of the DataGrid has the BackGround Color of the ColorSet Property of my Object.

My solution:
XAML

<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" >
<Setter.Value>
<Binding Path="State" Converter="{StaticResource BoolToSolidColorBrushConverter}">
<Binding.ConverterParameter>
<x:Array Type="SolidColorBrush">
<SolidColorBrush Color="Salmon"/>
<SolidColorBrush Color="Transparent"/>
</x:Array>
</Binding.ConverterParameter>
</Binding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>

c# (BoolToSolidColorBrushConverter)

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush[] solidColorBrushes;


if (parameter is SolidColorBrush[] && ((SolidColorBrush[])parameter).Length > 1)
{
solidColorBrushes = (SolidColorBrush[])parameter;
}
else
{
solidColorBrushes = new SolidColorBrush[] { new SolidColorBrush(Colors.Transparent), new SolidColorBrush(Colors.LightBlue) };
}


return (null == value || false == (bool)value) ? solidColorBrushes[1] : solidColorBrushes[0];
}