wpfenumsruntime-errorflagscompile-time-type-checking

How to pass multiple enum with flags values to a XAML value and still keeping compile time type checking


A partial answer exists here: WPF Multiple Enum Flags to Converter Parameter? where the proposed solution does loose the compile time type checking because it uses string values instead of real enum values. That could lead to error at runtime.

Additional information for experienced developers: This is the same problem that exists for many years about the INotifyPropertyChange before nameof exists, or lamda's for an alternate soso alternative.

Actually we can pass one enum parameter as: ConverterParameter={x:Static module:MyEnum.MyEnumValue} and still keeping compile time type checking.

But how to keep compile time type checking for multiples enum with flags values in XAML?

Example of what I would like to do:

<MultiDataTrigger.Conditions>
    <Condition Binding="{Binding Path=Item.SimulationNatureType}" 
        Value="{x:Static core:SimulationNatureType.TimeDomain | x:Static core:SimulationNatureType.FrequencyScan}">
    </Condition>
...

Solution

  • While

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

    would simply pass a string to the Condition's Value, the following should work:

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