Binding DynamicObject to RadGridView raises InvalidOperationException on some properties

1 Answer 167 Views
GridView
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Eldoir asked on 22 Jun 2022, 02:59 PM | edited on 22 Jun 2022, 03:03 PM

Hello,
I'm binding my grid to a collection of RowViewModel, which are basically DynamicObjects that maintain a Dictionary<string, CellViewModel>.
I've read this article to do this : WPF DataGrid | Data Bind to Dynamic Object with CLR and Dynamic Properties | Telerik UI for WPF (even if I'm not using CLR properties at the moment).

Now the thing is, for some rows and columns, it doesn't make sense having a cell (this is business-related).
So for these rows/columns, I don't create a new entry in my row dictionary. That leads to the following Get/Set methods inherited from DynamicObject:

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
    if (dic.ContainsKey(binder.Name))
    {
        result = dic[binder.Name];
        return true;
    }

    result = null;
    return false;
}

public override bool TrySetMember(SetMemberBinder binder, object value)
{
    if (dic.ContainsKey(binder.Name))
    {
        dic[binder.Name].Value = value;
        return true;
    }

    return false;
}

It seems to work well, there is no crash and it displays correctly (the concerned cells are empty).
However in my XAML Binding Failures, I have a bunch of these:

How to avoid this?
Thanks!

1 Answer, 1 is accepted

Sort by
1
Accepted
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
answered on 23 Jun 2022, 03:15 PM | edited on 23 Jun 2022, 03:15 PM

Answering my own question, actually in the MSDN doc about TryGet/SetMember:

So all I had to do was to return true in both methods instead of returning false, and the errors went away!

Martin Ivanov
Telerik team
commented on 27 Jun 2022, 06:23 AM

Thanks for sharing your solution, Arthur.
Tags
GridView
Asked by
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or