WPF Multiple Enum Flags to Converter Parameter?

柔情痞子 提交于 2021-02-19 04:18:38

问题


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


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/30270959/wpf-multiple-enum-flags-to-converter-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!