更改网格中行的背景颜色

问题描述 投票:1回答:2

我正在开发一个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事件处理程序执行此操作,但我没有在网格中看到此事件处理程序。

c# wpf xaml wpfdatagrid
2个回答
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();
    }
}

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内容混合到课堂中。更好地分离关注点。

© www.soinside.com 2019 - 2024. All rights reserved.