简体   繁体   中英

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

Similar problem to what's asked here , but it looks like the cause of this exception in this case is different. Perhaps I've set up my property wrong?

I've got a custom usercontrol, which I want to bind to. However, InitializeComponent throws the error Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable 1[System.Guid]' VS says it's the set property that's throwing this error.

I simplified my property to try and find the error, but it still happens without any setter logic

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

I'm binding to in the XAML using (client.BIllToClient is a Guid? )

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

Try this

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

I think you are trying to Bind simple CLR property . Binding require Target as DependencyProperty Custom binding .I have typeof(UserControl) as parent type in above code replace it with the Type of your control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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