WPF一个触发多个值

时间:2014-05-30 20:54:21

标签: wpf xaml

我尝试创建单个触发器将触发多个可能的选项。当选项是"已审核"我想将背景设置为绿色。或"已完成"。然后我想要第二个触发器,当" Pending"或者"黄色"。

这个答案指向了我,但它不完整,我无法理解它:https://stackoverflow.com/a/4660030/526704

这就是我现在所拥有的:

<Style x:Key="StatusCellTextBlock" TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="Text" Value="Reviewed">
            <Setter Property="Background" Value="Green"/>
        </Trigger>
        <Trigger Property="Text" Value="Completed">
            <Setter Property="Background" Value="Green"/>
        </Trigger>
        <Trigger Property="Text" Value="Pending">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
        <Trigger Property="Text" Value="Pending Review">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
    </Style.Triggers>
</Style>

这就是我正在寻找的东西:(某种方式指定触发相同制定者的属性的多个值。我有更多这些我想要的凝聚而不重复多次相同的触发器)

<Style x:Key="StatusCellTextBlock" TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="Text" Value="Reviewed" Value2="Completed">
            <Setter Property="Background" Value="Green"/>
        </Trigger>
        <Trigger Property="Text" Value="Pending" Value2="Pending Review">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
    </Style.Triggers>
</Style>

2 个答案:

答案 0 :(得分:0)

您可以创建将string转换为SolidColorBrush

的自定义IValueConverter
public class TextToBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch ((string)value)
        {
            case "Reviewed":
            case "Completed":
                return new SolidColorBrush(Colors.Green);
            case "Pending":
            case "Pending Review":
                return new SolidColorBrush(Colors.Yellow);
        }
        return DependencyProperty.UnsetValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后你甚至不需要Trigger。您可以将BindingConverter

一起使用
<Style x:Key="StatusCellTextBlock" TargetType="TextBlock">
    <Setter 
        Property="Background" 
        Value="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource TextToBackgroundConverter}}"/>
</Style>

其中TextToBackgroundConverter在资源中的某处定义为

<Window.Resources>
    <local:TextToBackgroundConverter x:Key="TextToBackgroundConverter"/>
</Window.Resources>

答案 1 :(得分:0)

我设计了一个解决方案。我现在正在使用转换器来确定是否在提供的参数中给出了值。我将选项"Reviewed,Completed"作为参数发送到转换器,如果在参数中的任何位置找到TextBlock中的文本,则返回true。

XAML:

    <Style TargetType="TextBlock" x:Key="StatusStyle">
        <Style.Triggers>
            <DataTrigger Value="True" Binding="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource OrConverter}, ConverterParameter=Reviewed;Completed}">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

转换器:

[ValueConversion(typeof(string), typeof(bool))]
public class MultiValueOrConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((string)parameter).Split(';').Contains((string)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}