简体   繁体   English

如何在WPF中进行条件DataTrigger

[英]How to make conditional DataTrigger in WPF

I wanna make coloring DataGridRow in my datagrid using style in WPF. 我想使用WPF中的style在datagrid中为DataGridRow着色。 I wanna make percentage of coloring based on value. 我想根据价值确定着色百分比。 If I have value binding Error between 0 and 50 it will be red. 如果我具有0到50之间的值绑定Error ,它将为红色。 And if vice versa it will be colored as Green 反之亦然,将其显示为绿色

But How I can do with style? 但是我该如何处理样式?

 <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Error}" Value="Error>50"> //maybe something like this
                        <Setter Property="Foreground" Value="#FFE08F8F" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Error}" Value="2">
                        <Setter Property="Foreground" Value="#FF6DBB6D" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>

You will need a custom converter that will convert Error to some value indicating the error state; 您将需要一个自定义转换器,该转换器将Error转换为指示错误状态的某个值。 the following converter will return True when Error is greater than 50: Error大于50时,以下转换器将返回True

public class ErrorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToInt(value) > 50;
    }

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

Now reference this in the resources (and True helper, you might not need this, can't remember if the conversion is automatic): 现在在资源中引用它( True帮助器,您可能不需要它,不记得转换是否是自动的):

<system:Boolean x:Key="True">True</system:Boolean>
<local:ErrorConverter x:Key="ErrorConverter">

And bind it up like this: 并像这样绑定它:

<Style TargetType="DataGridRow">
    <Style.Triggers>
        <DataTrigger 
            Binding="{Binding Error, Converter={StaticResource ErrorConverter}}" 
            Value="{StaticResource True}">
            <Setter Property="Foreground" Value="#FFE08F8F" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Error}" Value="2">
            <Setter Property="Foreground" Value="#FF6DBB6D" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Something along those lines should work. 遵循这些原则的东西应该起作用。

IMO the most flexible solution will be to add some kind of ErrorToColor converter. IMO最灵活的解决方案是添加某种ErrorToColor转换器。 Then use this XAML: 然后使用此XAML:

<Setter Property="Foreground" >
    <Setter.Value>
        <SolidColorBrush Color="{Binding Error, Converter={StaticResource errorToColorConverter}}" />
    </Setter.Value>
</Setter>

Such converter could look like this: 这样的转换器看起来像这样:

public class IntToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {            
        return value is int && ((int)value) > 50 ? Colors.Green : Colors.Red;
    }

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

This way you can easily manage colors for different error values. 这样,您可以轻松管理不同误差值的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM