我正在使用C#/ WPF应用程序。在其中一个xaml屏幕中,我有一个MS windows数据网格,我将自定义listview集合绑定到它。此listview集合(即MyCollection)包含各种产品的价格。集合类型为MyProduct:
public class MyProduct
{
public Int32 Id {get;set;}
public string Name {get;set;}
public Decimal Price {get;set;}
}
我需要根据价格值更改网格中一行的背景颜色。我该如何做到这一点?
我认为我可以使用RowDataBound事件处理程序执行此操作,但我不会在网格中看到此事件处理程序。
答案 0 :(得分:4)
以这样的样式设置DataGridRow
的背景:
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="dataGrid" Margin="55,29,44,43" ItemsSource="{x:Static local:MainWindow.FakeList}">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Price, Converter={x:Static local:MyPriceToBackgroundConverter.Instance}}"/>
</Style>
</DataGrid.Resources>
</DataGrid>
</Grid>
</Window>
窗口类:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static List<MyProduct> FakeList
{
get
{
return new List<MyProduct>
{
new MyProduct { Price = 5 },
new MyProduct { Price = 10 },
new MyProduct { Price = 20 }
};
}
}
}
转换器:
public class MyPriceToBackgroundConverter : IValueConverter
{
private static MyPriceToBackgroundConverter instance;
public static MyPriceToBackgroundConverter Instance
{
get
{
if (instance == null)
instance = new MyPriceToBackgroundConverter();
return instance;
}
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
decimal price = (decimal)value;
if (price > 8 && price < 12)
return Brushes.Red;
return Brushes.Azure;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Useless here
throw new NotImplementedException();
}
}
答案 1 :(得分:0)
执行此操作的一种方法是在InotifyPropertyChanged
上实施MyProduct
,然后添加一个包含您要为其着色的Brush
的属性。
public class MyProduct : INotifyPropertyChanged
{
protected int _Id;
public int Id
{
get
{
return this._Id;
}
set
{
if (this._Id == value)
{
return;
}
this._Id = value;
this.OnPropertyChanged();
}
}
//... And so on
protected decimal _Price;
public decimal Price
{
get
{
return this._Price;
}
set
{
if (this._Price == value)
{
return;
}
this._Price = value;
this.OnPropertyChanged();
this.OnPropertyChanged("MyColor");
}
}
public Brush MyColor
{
get
{
if( this._Price < 10)
{
return Brushes.Green;
}
}
else
{
//And so on
}
}
#region INPC
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = "")
{
PropertyChangedEventHandler tmp = this.PropertyChanged;
if (tmp != null)
{
tmp(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
对于你的DataGrid
,请执行以下操作将颜色绑定到背景:
<DataGrid ...>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding MyColor}"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
编辑: JYL的解决方案是另一种方法,因为您不需要额外的财产,但是您需要转换器,这可能是更好的解决方案。归结为偏好,但我建议你选择他的解决方案,因为我觉得它更干净,并且没有将UI内容混合到课堂中。更好地分离关注点。