WPF 将 DataGridTextColumn 的背景色绑定为按行着色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33309271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF bind background color of DataGridTextColumn to color by row
提问by user3685285
Say I have a DataGrid with the following data:
假设我有一个包含以下数据的 DataGrid:
John, Male
Mary, Female
Tony, Male
Sally, Female
The grid is bound to an ObservableCollection of Person model objects that implements INofifyPropertyChanged for the properties Person.Name and Person.Gender. I now want to bind the DataGridTextColumn's background color to the person's gender so that rows containing males are blue, and rows containing females are pink. Is it possible to do this by adding another property to the Person model like so:
网格绑定到 Person 模型对象的 ObservableCollection,该对象为属性 Person.Name 和 Person.Gender 实现了 INofifyPropertyChanged。我现在想将 DataGridTextColumn 的背景颜色绑定到人的性别,这样包含男性的行是蓝色的,包含女性的行是粉红色的。是否可以通过向 Person 模型添加另一个属性来做到这一点,如下所示:
public class Person
{
public Color BackgroundColor
{
get
{
if (gender == "Male")
{
return Color.Blue;
}
else
{
return Color.Pink;
}
}
}
if so, how do I bind this to the row or column's background color? I already have bounded columns like this:
如果是这样,我如何将它绑定到行或列的背景颜色?我已经有这样的有界列:
<DataGridColumn Header="Name" Binding={Binding Name} />
<DataGridColumn Header="Gender" Binding={Binding Gender} />
回答by dkozl
Assuming that BackgroundColoris of a System.Windows.Media.Colortype, and not System.Drawing.Color, if you want to change background of the entire row you can alter DataGrid.RowStyleand bind Backgroundproperty to BackgroundColorproperty
假设它BackgroundColor是一种System.Windows.Media.Color类型,而不是System.Drawing.Color,如果您想更改整行的背景,您可以更改DataGrid.RowStyle并将Background属性绑定到BackgroundColor属性
<DataGrid ...>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding Path=BackgroundColor}"/>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
回答by Todd Sprang
You want to implement an IValueConverter to convert a String to a Brush. See http://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/
您想要实现一个 IValueConverter 以将字符串转换为画笔。见http://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/
public class StringToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = (string)value;
return new SolidColorBrush(val == "male" ? Colors.Blue : Colors.Pink);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
In XAML, you'll want <Window.Resources>like
在 XAML 中,你会想要<Window.Resources>像
<Window.Resources>
<local:StringToBrushConverter x:Key="stringToBrush" />
<Style x:Key="MaleFemaleStyle" TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Path=Gender, Converter={StaticResource stringToBrush}}" />
</Style>
</Window.Resources>
Then apply the MaleFemaleStyle to your grid.
然后将 MaleFemaleStyle 应用到您的网格。
<DataGrid CellStyle="{StaticResource MaleFemaleStyle}">
...
</DataGrid>
回答by Robert Louis Everhard II
This works for me
这对我有用
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Sex}" Value="Male">
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
<DataTrigger Binding="{Binding Sex}" Value="Female">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>

