如何将带有标志值的多个枚举传递到XAML值并仍保持编译时类型检查

问题描述 投票:1回答:2

这里有一个部分答案:WPF Multiple Enum Flags to Converter Parameter?,其中所提出的解决方案确实松散了编译时类型检查,因为它使用字符串值而不是实际枚举值。这可能会在运行时导致错误。

为经验丰富的开发人员提供的其他信息:这与在nameof存在之前的INotifyPropertyChange存在多年的相同问题,或者对于替代的soso替代品来说都是lamda的问题。

实际上,我们可以将一个枚举参数传递为:ConverterParameter = {x:Static module:MyEnum.MyEnumValue},并且仍然保持编译时类型检查。

但是如何在XAML中继续对带有标志值的倍数枚举进行编译时类型检查?

我想做的事的例子:

<MultiDataTrigger.Conditions>
    <Condition Binding="{Binding Path=Item.SimulationNatureType}" 
        Value="{x:Static core:SimulationNatureType.TimeDomain | x:Static core:SimulationNatureType.FrequencyScan}">
    </Condition>
...
wpf enums runtime-error flags compile-time-type-checking
2个回答
1
投票

同时

<MultiDataTrigger.Conditions>
    <Condition ... Value="TimeDomain,FrequencyScan">
    </Condition>
</MultiDataTrigger.Conditions>

只需将字符串传递给条件的值,以下方法应该起作用:

<MultiDataTrigger.Conditions>
    <Condition ...>
        <Condition.Value>
            <core:SimulationNatureType>TimeDomain,FrequencyScan</core:SimulationNatureType>
        </Condition.Value>
    </Condition>
</MultiDataTrigger.Conditions>

0
投票

就像其他信息一样,这就是答案对我的帮助:

((我知道我的通用转换器不好...对不起... 1 = true,2 = false,4 = null)

<Style TargetType="Image" x:Key="ImageStyleForSimulatedInfoSimulationValues">
            <Style.Setters>
                <Setter Property="Source" Value="pack://application:,,,/WpfUtil;component/Images/NotApplicable16x16.png"></Setter>
            </Style.Setters>

            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Value="True">
                            <Condition.Binding>
                                <Binding Path="Item.SimulationNatureType" Converter="{StaticResource EnumWithFlagsHavingSomeSpecificFlagSetToBoolean}">
                                    <Binding.ConverterParameter>
                                        <core:SimulationNatureType>TimeDomain, FrequencyScan</core:SimulationNatureType>
                                    </Binding.ConverterParameter>
                                </Binding>
                            </Condition.Binding>
                        </Condition>
                        <Condition Value="True" Binding="{Binding Path=Item.IsValidForValues, ConverterParameter=1,
                                                Converter={StaticResource NullableBooleanToBooleanUniversalConverter}}">
                        </Condition>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Source" Value="pack://application:,,,/WpfUtil;component/Images/OkSmall16x16.png" />
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Value="True">
                            <Condition.Binding>
                                <Binding Path="Item.SimulationNatureType" Converter="{StaticResource EnumWithFlagsHavingSomeSpecificFlagSetToBoolean}">
                                    <Binding.ConverterParameter>
                                        <core:SimulationNatureType>TimeDomain, FrequencyScan</core:SimulationNatureType>
                                    </Binding.ConverterParameter>
                                </Binding>
                            </Condition.Binding>
                        </Condition>
                        <Condition Value="True" Binding="{Binding Path=Item.IsValidForValues, ConverterParameter=6,
                                                Converter={StaticResource NullableBooleanToBooleanUniversalConverter}}">
                        </Condition>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Source" Value="pack://application:,,,/WpfUtil;component/Images/Warning16x16.png" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
© www.soinside.com 2019 - 2024. All rights reserved.