Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}?

like image 425
Will Avatar asked Dec 25 '22 19:12

Will


1 Answers

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>
like image 152
Will Avatar answered Dec 27 '22 20:12

Will