2

I have DependencyProperty State of Flags enum type. I want to change some element border color due to changing of the State property. For some reasons I can not operate with the element directly but only by setting its Style.

How can I change following code to make it possible not to check exact value of State but check whether it contains desired flag?

<Style.Triggers>
            <Trigger Property="State" Value="None">
                <Setter Property="StateBorderBrush" Value="Transparent"/>
            </Trigger>
            <Trigger Property="State" Value="Covered">
                <Setter Property="StateBorderBrush" Value="Blue"/>
            </Trigger>
            <Trigger Property="State" Value="Selected">
                <Setter Property="StateBorderBrush" Value="Red"/>
            </Trigger>
            <Trigger Property="State" Value="contains flag 'Controlled'">
                <Setter Property="StateBorderBrush" Value="Orange"/>
            </Trigger>
        </Style.Triggers>
4

4 回答 4

2

就我而言,唯一的解决方案是转换器:

 public class EnumFlagConverter : ValueConverter
{
    public string FlagValue { get; set; }
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Enum en = value as Enum;
        var enumFlag = Enum.Parse((Type)parameter, FlagValue);
        return en.HasFlag((Enum)enumFlag);
    }
}

和 xaml 绑定:

 <DataTrigger Binding="{Binding State,Converter={cnv:EnumFlagConverter FlagValue='Locked'}, ConverterParameter={x:Type viewModels:SourceControlState}}"
                         Value="True">
                <Setter Property="Source"
                        TargetName="srcCtrlStatusIndicator"
                        Value="{StaticResource ImageSourceCheckedOutBySomeoneElse9x9}" />                  

            </DataTrigger>
于 2016-07-10T12:18:09.463 回答
1

在此处查看 Lars 对类似问题的通用答案:使用通用转换器

他的解决方案适用于不互斥的枚举标志。这让我可以使用这个 XAML。

<DataTrigger Binding="{Binding Path=State, Converter={StaticResource EnumBooleanConverter}, ConverterParameter={x:Static enums:MyStatesEnum.MyEnumFlagValue}}" Value="True">
                <Setter Property="BorderBrush" Value="Red"/>
            </DataTrigger>
于 2015-04-15T02:15:41.377 回答
0

最后我通过使用 DataTrigger 与 Self 的相对绑定来实现它:

<DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Self}}" Value="None">
      <Setter Property="StateBorderBrush" Value="Transparent"/>
</DataTrigger>

然后可以扩展绑定元素并应用适当的转换器

于 2015-01-15T07:01:00.997 回答
0

这是您在触发器中测试枚举的方式:

    <ControlTemplate.Triggers>
        <Trigger Property="ViewState"
                 Value="{x:Static constants:LicenseViewState.License}">
            <Setter Property="Visibility"
                    Value="Collapsed"
                    TargetName="ProductComboBoxField">
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>
于 2017-03-31T21:32:36.193 回答