繁体   English   中英

类型为“ System.Windows.Data.Binding”的对象不能转换为类型为“ System.Nullable`1 [System.Guid]”

[英]Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable`1[System.Guid]'

此处询问的问题类似,但在这种情况下,导致此异常的原因似乎有所不同。 也许我设置的财产有误?

我有一个要绑定的自定义用户控件。 但是,InitializeComponent引发错误Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable 1[System.Guid]' VS表示引发此错误的是set属性。

我简化了属性以尝试查找错误,但是在没有任何设置逻辑的情况下仍然会发生

public Guid? SelectedItemGUID
{
    get
    {
        if (SelectedItem == null) return null;
        else return (Guid)SelectedItem.GetType().GetProperty("GUID").GetValue(SelectedItem, null);
    }
    set
    {
        return;
    }
}

我正在使用(client.BIllToClient是Guid?

SelectedItemGUID="{Binding Path=client.BillToClient, Mode=TwoWay}"

尝试这个

public static readonly DependencyProperty SelectedItemGUIDProperty = DependencyProperty.Register("SelectedItemGUID",typeof(Guid?),typeof(UserControl),new FrameworkPropertyMetadata(null));

    public Guid? SelectedItemGUID
    {
        public Guid? SelectedItemGUID
    {
        get { return (Guid?)GetValue(SelectedItemGUIDProperty); }
        set { SetValue(SelectedItemGUIDProperty, (Guid?)SelectedItem.GetType().GetProperty("GUID").GetValue(SelectedItem, null)); }
    }
    }

我认为您正在尝试绑定简单的CLR属性。 绑定需要Target作为DependencyProperty 自定义绑定 。在上面的代码中,我将typeof(UserControl)作为父类型替换为控件的Type。

暂无
暂无

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

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