根据条件WPF更改单元格的前景色绑定

时间:2018-10-26 08:56:21

标签: c# wpf

我正在尝试根据值是否超过设置的限制将单元格的默认颜色更改为红色。当前,正在发生的事情是我有一个带有复选框的油列表的网格。选中任何特定油的复选框后,该油将添加到另一个网格中,我可以在其中操纵其数量。例如,如果我检查1种油,则其“ UsagePercentage”变为100。如果我检查另一种油,则百分比将更新为50/50。从那里,如果我单击一个按钮将其值增加1,它将更改为66.6 / 33.3,依此类推。

我要根据我的代码尝试实现的是,如果油的UsagePercentage大于UsageLimit,请将前景文本更改为红色,否则将其保留为浅灰色。但是,当我运行程序时,UsagePercentage文本为黑色。我不明白为什么绑定UsagePercentage起作用,但是ForegroundColor不能起作用。

Oil.cs

public float UsagePercentage
    {
        get { return mUsagePercentage; }
        set
        {
            mUsagePercentage = value;
            NotifyPropertyChanged("UsagePercentage");
        }
    }

OilList.cs

public void UpdateFormulaWeight()
    {
        float ftw = mOils.Sum(oil => oil.TotalWeight);
        mFormulaTotalWeight = ftw;

        NotifyPropertyChanged("UsagePercentage");

        foreach (Oil oil in mOils)
        {
            oil.UsagePercentage = (oil.TotalWeight / mFormulaTotalWeight) * 100f;

            if (oil.UsagePercentage > oil.UsageLimit)
                ForegroundColor = "Red";
            else
                ForegroundColor = "LightGray";

            NotifyPropertyChanged("ForegroundColor");
            NotifyPropertyChanged("UsagePercentage");               
        }
    }

private string ForegroundColor
    {
        get { return mForegroundColor; }
        set
        {
            mForegroundColor = value;
            NotifyPropertyChanged("ForegroundColor");
        }
    }

GrayToRed.cs

public class GrayToRed : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string input = value as string;

        switch(input)
        {
            case "Red":
                return Brushes.Red;
            case "LightGray":
                return Brushes.LightGray;
            default:
                return DependencyProperty.UnsetValue;
        }
    }

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

}

MainWindow.xaml

<Window.Resources>
    <converters:GrayToRed x:Key="GrayToRed" />
</Window.Resources>
...
<!-- USAGE PERCENTAGE -->
            <DataGridTextColumn Header="Formula %"  Binding="{Binding UsagePercentage,  StringFormat={}{0:F2}%}" IsReadOnly="True" Width="64">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="#1e1e1e"/>
                        <Setter Property="Foreground" Value="{Binding ForegroundColor, Converter={StaticResource GrayToRed}}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>

                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="TextBlock.VerticalAlignment" Value="Center" />
                        <Setter Property="TextBlock.TextAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

0 个答案:

没有答案
相关问题