简体   繁体   English

WPF绑定以控制可见性“数字不是有效的枚举值”

[英]WPF Binding to control visiblity “Numbers are not valid enumeration Values”

I am trying to set up the visibility property on a control so that it is visible when the bound value is matches an arbitrary value. 我试图在控件上设置可见性属性,以便在绑定值与任意值匹配时可见。

I have set up my converter as a static resource 我已将转换器设置为静态资源

Applied the binding 应用绑定

<Button Content="Foo" Visibility="{Binding SelectedValue, Converter={StaticResource ValueToVisibilityConverter}, ConverterParameter='1,2'}" />

But am met with the error 但是我遇到了错误

Error 1 '{Binding SelectedValue, Converter={StaticResource ValueToVisibilityConverter}, ConverterParameter='1,2'}' cannot be used as a value for 'Visibility'. 错误1'{Binding SelectedValue,Converter = {StaticResource ValueToVisibilityConverter},ConverterParameter ='1,2'}'不能用作'Visibility'的值。 Numbers are not valid enumeration values. 数字不是有效的枚举值。

My converter code is 我的转换器代码是

public class ValueToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
        {
            if (value == null || parameter == null || !(value is String))
                return Visibility.Collapsed;

            var currentValue = value.ToString();
            var matchStrings = parameter.ToString();
            var found = false;

            foreach (var state in matchStrings.Split(','))
            {
                found = (currentValue == state.Trim());

                if (found)
                    break;
            }

            return found ? Visibility.Visible : Visibility.Collapsed;
        }

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

The error stops compile and feels like it is trying to be too clever and is ignoring my converter. 该错误停止编译,并感觉像是它试图变得太聪明了,而忽略了我的转换器。

Have I applied it wrong or am otherwise ignorant of some process going on. 我是否将其应用错误或对正在进行的某些过程一无所知。

EDIT: 编辑:

To get the converter as a static resource I have the below in my window definition 为了将转换器作为静态资源,我在窗口定义中包含以下内容

xmlns:myConverters="clr-namespace:<namespace>;assembly=<assemblyname>"

And this in my window resources, right along side the same code for other converters that work perfectly 而这在我的窗口资源中,与其他完美运行的转换器的代码相同

<myConverters:ValueToVisibilityConverter x:Key="ValueToVisibilityConverter" />

This is the code that should work. 这是应该起作用的代码。

<Button Content="Foo" 
        Visibility="{Binding SelectedValue, 
                     Converter={StaticResource ValueToVisibilityConverter}, 
                     ConverterParameter=1|2}" />

Things that you need to do 您需要做的事情

  1. Values in ConverterParameter are passed without any quotes. 传递ConverterParameter中的值时不带引号。 So remove single quotes from converter parameter. 因此,请从转换器参数中删除单引号。

  2. There is nothing stopping your from sending more than one value into the parameter, as long as you have a delimiter to separate them out later, but you cannot use a comma as that delimits the XAML. 只要您有一个定界符以后再将它们分开,就不会阻止您向该参数发送多个值, 但是您不能使用逗号来分隔XAML。 So use pipe in such cases and in converter split parameter by pipe | 因此,在这种情况下以及在管道中的转换器拆分参数中使用管道| .

Moreover, please note that 此外,请注意
a) There has to be a static resource for converter like this in resources. a)资源中必须有一个静态资源供此类转换器使用。

<local:ValueToVisibilityConverter x:Key="ValueToVisibilityConverter" />

where local is xmlns:local="Your project in which this converter is defined" 其中localxmlns:local="Your project in which this converter is defined"

Note: A trick eBay used to use in urls, years ago, was to delimit data in the URL with QQ. 注意:几年前,eBay在URL中使用的一个技巧是用QQ分隔URL中的数据。 A double-Q does not naturally occur in text data. 文本数据中自然不会出现double-Q。 If you ever get stuck for a text delimiter that will avoid encoding issues just use QQ... This will not work with split though (which requires single characters, but nice to know) :) 如果您曾经为避免使用编码定界符而避免编码问题而苦恼,则只需使用QQ ...但这将无法使用split(它需要单个字符,但很高兴知道):)

you can pass the ConverterParameter like 您可以像这样传递ConverterParameter

  <Binding Path="MyProperty"
             Converter="{StaticResource IntToBoolConverter}">
        <Binding.ConverterParameter>
            <sys:Int32>0</sys:Int32>
        </Binding.ConverterParameter>
    </Binding>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM