[英]Changing background color of the row in a grid
我正在使用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事件處理程序來執行此操作,但是在網格中看不到該事件處理程序。
像這樣設置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();
}
}
一種方法是在MyProduct
上實現InotifyPropertyChanged
並添加一個包含要為其着色的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混入類中。 更好地分離關注點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.