Binding PropertyGrid to DynamicObject classes

4 posts, 0 answers
  1. Matt
    Matt avatar
    14 posts
    Member since:
    May 2012

    Posted 31 Oct 2012 Link to this post

    I have a scenario in which I have a ViewModel of a particular type that can hold a Model that can be one of a set of types.

    The ViewModel inherits from DynamicObject to expose elements within the Model as properties for the PropertyGrid. In my DynamicObject implementation I am overriding GetDynamicMemberNames, TryGetMember, and TrySetMember. I am keeping my own dictionary of member names and accessors within my ViewModel.

    The underlying Model may have different members so that each instance of the ViewModel should appear to have different properties.

    I have a list of instances of the ViewModel and the PropertyGrid is bound to the list's selected item. When I switch items, WPF databinding makes calls to TryGetMember within the newly selected ViewModel. However, when that set of calls are made, they're based on the member names returned by the previous instance.

    So let's say I have Model1 with properties A, B, C and Model 2 with properties D, E, F. Instances of either can be held in an instance of my dynamic ViewModel. I add an instance of each to the list. I select the item containing the instance of Model1, the PropertyGrid shows three properties A, B, C just fine. When I select the item containing Model2 however, I get an error within my TryGetMember saying that the key isn't found, I look at the binder.Name and it's reading "A" even though this new instance of ViewModel has a model of type Model2 with only the properties D, E, F.

    When I switch between these two items, GetDynamicMemberNames() isn't called more than once. I suspect that somewhere along the way the member names are being cached by the name of the type they're associated with. So in this internal cache someplace it's associating my ViewModel class with properties A, B, C and uses that cache to query all instances of ViewModel.

    My questions are:

    1) Do you know where this caching is happening? Is this in PropertyGrid, in WPF databinding, or within the DynamicObject base class?

    2) Is there any way to override the member name caching?

    3) Is there any way to force a call to GetDynamicMemberNames() each time a new instance of my ViewModel is loaded into the PropertyGrid?

    Thanks for your help.

    Matt
  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 01 Nov 2012 Link to this post

    Hello Matt,

    As I have replied to the colleague of yours, this is a undesired behavior, so that we will disable the item caching logic for dynamic types. The change will be available with next week's IB.

    Regards,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Franck
    Franck avatar
    2 posts
    Member since:
    Nov 2012

    Posted 07 Jan 2013 Link to this post

    Hello everybody.

    Would you mind if I ask for the answer of the 3rd question ? :)

    I have the same problem: Adding the first item to the collection call GetDynamicMemberNames.
    If I add more columns, GetDynamicMemberNames isn't being called again and I can't edit values in that new columns (commited values call TryGetMember (instead of TrySetMember) and thus, put the old value in the cell).

    Any help would be VERY appreciated since i've spent so many days on this prob.

    Regards,

    Franck
  5. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 10 Jan 2013 Link to this post

    Hello Franck,

    Would it be possible for you to send us a repro project so that we could debug it on our side and give you an appropriate solution?

    Kind regards,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top