WPF DataGrid基于规则的几个列的颜色

时间:2014-05-31 10:39:13

标签: c# wpf gridview

我开始开发WPF应用程序,现在我已经集成了一些带有一些十进制值的DataGrid。这些值类似于EBIT-Marge,RoE等。

现在,我想知道如果EBIT-Marge超过12,如何为细胞着色,以及如果RoE低于20,如何为细胞着色。

当我使用Grid开发ASP.NET时,我使用OnRowDataBound-Event来完成这些工作。

有什么想法吗?

祝你好运, Rayk

1 个答案:

答案 0 :(得分:0)

您应该使用转换器执行此操作(msdn)。

示例:

ColorConverter转换器:

class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && parameter != null)
        {
            double? dValue = value as double?;
            if (!dValue.HasValue) return Binding.DoNothing;

            var items = parameter.ToString().Split(' ');
            if(items.Length == 2)
            {
                double cValue;
                if(!double.TryParse(items[1], out cValue))
                    return Binding.DoNothing;

                switch (items[0])
                {
                    case "<":
                        if (dValue.Value < cValue)
                            return new SolidColorBrush(Colors.Red);
                        break;
                    case ">":
                        if (dValue.Value > cValue)
                            return new SolidColorBrush(Colors.Red);
                        break;
                    default:
                        break;
                }
            }
        }
        return Binding.DoNothing;
    }

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

Convrter ColorConveter将您的表达式作为参数字符串,例如&#39;&GT; 12&#39;或&#39;&lt; 20&#39;

XAML代码:

...
<Window.Resources>
    <local:ColorConverter x:Key="colorConverter" />
</Window.Resources>
<Grid>
    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding EBIT}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="{Binding EBIT, Converter={StaticResource colorConverter}, ConverterParameter='&gt; 12'}"/>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding RoE}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="{Binding RoE, Converter={StaticResource colorConverter}, ConverterParameter='&lt; 20'}"/>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
...

解决方案2:

数据类:

class Data : INotifyPropertyChanged
{
    private double _RoE;
    public double RoE
    {
        get { return _RoE; }
        set 
        { 
            _RoE = value;
            if (value < 10)
                RoEColor = new SolidColorBrush(Colors.Red);
            else if (value > 20)
                RoEColor = new SolidColorBrush(Colors.Green);
            else
                RoEColor = new SolidColorBrush(Colors.White);
            OnPropertyChanged("RoE");
        }
    }

    private SolidColorBrush _RoEColor;
    public SolidColorBrush RoEColor
    {
        get { return _RoEColor; }
        set { _RoEColor = value; OnPropertyChanged("RoEColor"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }
}

现在您应该将RoEColor绑定到TextBlock背景属性。

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
    <DataGrid.Columns>               
        <DataGridTextColumn Binding="{Binding RoE}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Background" Value="{Binding RoEColor}"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>