Search code examples
c#wpfenumsflagsvalueconverter

WPF Multiple Enum Flags to Converter Parameter?


I have a control which I need visible if an enum value is (A | B | C).

I know how to bind the visibility of a control to a SINGLE enum (A) using a converter.

How do I go about doing the same for this case? What would go in the parameter?

This is the converter I use :

public class EnumToVisibilityConverter : IValueConverter {
    public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
        if ( value == null || parameter == null || !( value is Enum ) )
            return Visibility.Hidden;
        string State = value.ToString( );
        string parameterString = parameter.ToString( );

        foreach ( string state in parameterString.Split( ',' ) ) {
            if ( State.Equals( state ) )
                return Visibility.Visible;
        }
        return Visibility.Hidden;
    }

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

This is the XAML Binding :

<UserControl.Visibility>
    <Binding
        Path="GameMode" Source="{x:Static S:Settings.Default}" Converter="{StaticResource ETVC}"
        ConverterParameter="{x:Static E:GameMode.AudiencePoll}" Mode="OneWay"/>
</UserControl.Visibility>

How would do I pass (A|B|C) to the Converter Parameter? Is it as simple as just saying {x:Static E:Enum.A | E:Enum.B | E:Enum.C}?


Solution

  • I was able to find the answer here

    To save everyone a trip

    <Binding Path="PathGoesHere" Source="{x:Static SourceGoesHere}" Converter="{StaticResource ConverterKeyGoesHere}">
        <Binding.ConverterParameter>
            <EnumTypeGoesHere>A,B,C</EnumTypeGoesHere>
        </Binding.ConverterParameter>
    </Binding>