输出中的多重绑定错误

发布于 2024-11-19 18:35:34 字数 2615 浏览 2 评论 0原文

我有一个根据两个属性启用的按钮。我使用了带有转换器的多重绑定。

一切正常,但输出一直显示:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='' MultiBindingExpression:target element is 'HGCCommandButton' (Name='btnEliminar'); target property is 'IsEnabled' (type 'Boolean')

这里有一些问题具有类似的问题: WPF 多重绑定失败。为什么?

任何简单的解决方案或者我应该在 ViewModel 中创建逻辑并仅绑定到一个属性?

代码: XAML:

<utils:HGCCommandButton x:Name="btnEliminar">
                    <utils:HGCCommandButton.IsEnabled>
                        <MultiBinding Converter="{StaticResource MultiValueIsEnabledConverter}"
                                      ConverterParameter="NotEnabledIfIsFromInfoGestionOrIsNew">
                            <Binding Path="IsNew" />
                            <Binding Path="IsAbonado" />
                        </MultiBinding>
                    </utils:HGCCommandButton.IsEnabled>
                </utils:HGCCommandButton>

转换器:

public class MultiValueIsEnabledConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter!=null)
            {
                if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) return "";

                var sel = (MultiValueIsEnabledConverterNames)Enum.Parse(typeof(MultiValueIsEnabledConverterNames), parameter.ToString());

                switch (sel)
                {
                   ...

                    case MultiValueIsEnabledConverterNames.NotEnabledIfIsFromInfoGestionOrIsNew:
                        return (bool)NotEnabledIfIsFromInfoGestionOrIsNew(values[0], values[1]);

                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }

            return false;
        }
    private static object NotEnabledIfIsFromInfoGestionOrIsNew(object isFromIG, object isNew)
            {
                if ((isFromIG != null) && !(bool)isFromIG)
                {
                    if ((isNew != null) && !(bool)isNew) 
                    {
                        return !((bool)isFromIG && (bool)isNew);
                    }
                    return false;
                }
                return false;
            }

ViewModel 变量只是两个布尔值

I have a button that is enabled depending on two properties. I used a MultiBinding with a Converter.

Everything works but the output keeps saying:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='' MultiBindingExpression:target element is 'HGCCommandButton' (Name='btnEliminar'); target property is 'IsEnabled' (type 'Boolean')

There are some questions here with similar issues:
WPF MultiBinding Fails. Why?

Any easy solution or I should make the logic in the ViewModel and bind just to one property?

CODE:
XAML:

<utils:HGCCommandButton x:Name="btnEliminar">
                    <utils:HGCCommandButton.IsEnabled>
                        <MultiBinding Converter="{StaticResource MultiValueIsEnabledConverter}"
                                      ConverterParameter="NotEnabledIfIsFromInfoGestionOrIsNew">
                            <Binding Path="IsNew" />
                            <Binding Path="IsAbonado" />
                        </MultiBinding>
                    </utils:HGCCommandButton.IsEnabled>
                </utils:HGCCommandButton>

Converter:

public class MultiValueIsEnabledConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter!=null)
            {
                if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) return "";

                var sel = (MultiValueIsEnabledConverterNames)Enum.Parse(typeof(MultiValueIsEnabledConverterNames), parameter.ToString());

                switch (sel)
                {
                   ...

                    case MultiValueIsEnabledConverterNames.NotEnabledIfIsFromInfoGestionOrIsNew:
                        return (bool)NotEnabledIfIsFromInfoGestionOrIsNew(values[0], values[1]);

                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }

            return false;
        }
    private static object NotEnabledIfIsFromInfoGestionOrIsNew(object isFromIG, object isNew)
            {
                if ((isFromIG != null) && !(bool)isFromIG)
                {
                    if ((isNew != null) && !(bool)isNew) 
                    {
                        return !((bool)isFromIG && (bool)isNew);
                    }
                    return false;
                }
                return false;
            }

ViewModel variables are just two booleans

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2024-11-26 18:35:34

这是一个相当奇怪的 ConverterParameter,您为 Converter 引用的 StaticResource 实际上是您发布其代码的转换器的实例吗?该错误声称该值不是布尔值,并且该方法单独只能返回布尔值,因此我怀疑在这里找到了错误。

Convert 方法中的代码可以返回除 bool 之外的其他值吗?如果可能的话,您需要避免它。

编辑:问题就在这里:

if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) return "";

您返回一个与布尔属性不兼容的空字符串,

That is quite an odd ConverterParameter, is the StaticResource you reference for the Converter actually an instance of the converter whose code you posted? The error claims that the value is not a bool and that method alone can only return a bool so i doubt that the error is found here.

Can the code in your Convert method return anything else but a bool? You need to avoid it if that is possible.

Edit: Right here is the problem:

if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) return "";

You return an empty string which is not compatible with the boolean property,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文