4

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

2 Answers 2

10

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>
1

as outlined here the syntax should be

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

as the comma seperates the XML and the parameter would always be one single enum value. there is no intellisense though

Not the answer you're looking for? Browse other questions tagged or ask your own question.