【问题标题】:WPF Binding to control visiblity "Numbers are not valid enumeration Values"WPF绑定以控制可见性“数字不是有效的枚举值”
【发布时间】:2014-08-22 18:50:01
【问题描述】:

我正在尝试在控件上设置可见性属性,以便在绑定值与任意值匹配时可见。

我已将转换器设置为静态资源

应用绑定

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

但我遇到了错误

错误 1 ​​'{绑定 SelectedValue, Converter={StaticResource ValueToVisibilityConverter}, ConverterParameter='1,2'}' 不能使用 作为“可见性”的值。数字不是有效的枚举值。

我的转换器代码是

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();
        }
    }

错误停止编译,感觉好像太聪明了,忽略了我的转换器。

是我应用错了还是不知道正在进行的某些过程。

编辑:

要将转换器作为静态资源,我的窗口定义中有以下内容

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

这在我的窗口资源中,与其他完美运行的转换器的代码相同

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

【问题讨论】:

  • 为什么要传递 ConverterParameter='1,2' ?

标签: c# wpf binding ivalueconverter


【解决方案1】:

这是应该工作的代码。

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

你需要做的事情

  1. ConverterParameter 中的值不带任何引号传递。所以从转换器参数中删除单引号。

  2. 没有什么可以阻止您将多个值发送到参数中,只要您以后有分隔符将它们分开,但您不能使用逗号分隔 XAML。 所以在这种情况下使用管道,并在转换器拆分参数中按管道|

此外,请注意
a) 资源中必须有这样的转换器的静态资源。

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

localxmlns:local="Your project in which this converter is defined"

注意: 多年前 eBay 在 url 中使用的一个技巧是用 QQ 分隔 URL 中的数据。文本数据中自然不会出现双 Q。如果您遇到避免编码问题的文本分隔符卡住了,请使用 QQ……这不适用于拆分(它需要单个字符,但很高兴知道):)

【讨论】:

  • 然后我得到错误The unnamed argument "2" must appear before named arguments.
  • @Hugoagogo:阅读我更改后的答案。这肯定会解决您的问题。
  • 感谢您发布评论的链接让我走上了正确的轨道,我尝试了您所做的,我只是在追赶一个次要错误(!(value is String) 应该从我的转换器中删除)当您发布了更新的答案。感谢易趣提示
  • 顺便说一句,我已经使用了管道,但为了将来参考,是否有一种简单的方法可以逃避逗号。
【解决方案2】:

你可以像这样传递 ConverterParameter

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

【讨论】:

  • 我在问题中提到我需要能够检查与任意值的匹配,我不想为我需要匹配的任何值组合创建许多几乎相同的转换器。跨度>
  • 如何将其扩展到多个值
猜你喜欢
  • 2018-12-28
  • 2011-07-27
  • 2011-04-12
  • 2015-01-23
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
相关资源
最近更新 更多