Confused about RadPropertyGrid DropDownList editors

10 posts, 0 answers
  1. Travis
    Travis avatar
    49 posts
    Member since:
    Dec 2012

    Posted 13 May 2013 Link to this post

    Hello,

    I've asked this before, and I'm still a bit confused how this all wires up. The confusion is mine, over what the Telerik RadPropertyGrid type checking is actually doing.

    Let's say we've got a model that looks like this. These all would implement INotifyPropertyChanged and such, but for example, I'm leaving that off.

    class Dog {
      public string Name{get;set;}
    }
     
    class Shelter {
      public Dog AdoptedDog{get;set;}
      public List<Dog> AvailableDogs{get;set;}
    }

    Further, I am adapting the model into the view (RadPropertyGrid DropDownList editors) through a view model something like this:

    abstract class ModelView<T> {
    public T Item{get;set;}
      public ModelView(T item) {
        Item = item;
      }
    }
     
    class DogModelView : ModelView<Dog> {
      public DogModelView(Dog dog)
        : base(dog) {}
    }
     
    class ShelterModelView : ModelView<Shelter> {
      public ShelterModelView(Shelter shelter)
        : base(shelter) {}
    }

    I need to be able to edit the AdoptedDog property from the Shelter (adapter through ShelterModelView) with a DropDownList that shows the AvailableDogs to choose from.

    So we set the RadPropertyGrid SelectedObject to the ModelView instance:

    //Shelter AvailableDogs might be populated with Dogs of course.
    var shelter = new Shelter();
    radpgShelter.SelectedObject = new ShelterModelView(shelter)

    And when the editor is initialized:

    //...
    element.DataSource = shelter.AvailableDogs;
    //Following which the SelectedValue is indeed selected.

    Am I assuming correctly that the RadPropertyGrid type check wants to connect an item from the DataSource, in particular the SelectedValue, to agree in type with the ModelView property being edited?

    So in this example, if we want to edit (select from a DropDownList) the AdoptedDog from a list of AvailableDogs, then we should be talking about Dog and Dog? Rather, not, say, Dog and string (Dog.Name)?

    Thank you...

    Regards,

    Michael Powell
  2. Travis
    Travis avatar
    49 posts
    Member since:
    Dec 2012

    Posted 13 May 2013 Link to this post

    To clarify the confusion, I populate DataSource with a List<T> of the Type T being edited in the RadPropertyGrid behind the DropDownList editor DataSource. I press enter in the list of available items, and the edit doesn't want to go, it never makes it to the ModelView property setter as I expect it to. It's been mentioned in other forum posts something about the type check, but I don't have a clear idea what is being type checked to resolve the issue.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Travis
    Travis avatar
    49 posts
    Member since:
    Dec 2012

    Posted 13 May 2013 Link to this post

    I am trying different things, and in a prior entry, I'd identified SelectedValue.GetType() != ((PropertyGridItem) e.Item).Value.GetType(), however, in this case, the types are both the same: from the above demo model, both would be typeof(Dog). What am I doing wrong? As soon as I set DataSource, SelectedValue is correct, so I'm fairly certain the selection is taking place. I just can't seem to get the edit to complete, pressing enter doesn't do anything and leaves the ModelView property unchanged.
  5. Travis
    Travis avatar
    49 posts
    Member since:
    Dec 2012

    Posted 13 May 2013 Link to this post

    New information: Editor ValueChanging is in fact trying to change the value: OldValue = "Labrador" (a Dog) and NewValue = "Collie" (a Dog), both types agree, so I know it is trying to get it done.
  6. Travis
    Travis avatar
    49 posts
    Member since:
    Dec 2012

    Posted 14 May 2013 Link to this post

    This wouldn't take a Zip or 7-zip file, so I submitted a support ticket. Probably I need a custom type converter is my guess, but some help identifying the right solution would be grand. Thanks!

    *Edit* It would seem I can emulate the desired behavior by changing the SelectedObject (as ShelterView) property in the Editor ValueChanging event handler. However, this circumvents other functionality, like the ability to reset to the previous value.

    If there's another or better way I should be doing this, or it's a bug, would you let me know? Thank you...
  7. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 15 May 2013 Link to this post

    Hello Michael,

    Thank you for writing.

    RadPropertyGrid uses the TypeConverter of the type of the property you are editing and uses it to convert values from their string representation in the editor to values that can be assigned to the actual property type. In your case you are using the default TypeConverter which is unable to convert the string value of the editor to an object of your "AvailableDogs" collection. What you need is to return the actual object that is bound in the drop down editor as the value of the editor. This way the converter will not be used as the object coming from the editor is directly assignable to the property type (they are practically of the same type). To do this you will need a custom drop down editor. In that custom editor you have to override the getter of the Value property and return the data bound object of the drop down element. Then you can use the editor required event to tell the property grid to use your editor. I have attached a modified version of your project where I have implemented this editor.

    I hope this will be useful. Should you have further questions ,I would be glad to help.

    Greetings,
    Ivan Petrov
    the Telerik team
    RadChart for WinForms is obsolete. Now what?
  8. Travis
    Travis avatar
    49 posts
    Member since:
    Dec 2012

    Posted 15 May 2013 Link to this post

    I've got a workaround solution in place, but obviously if we can work directly with the type(s) being listed and/or selected, that's far and away preferred. Will look into the TypeConverter solution: is there a minimal example what we need to override for a domain object type-specific TypeConverter? In prior experience I seem to recall that included returning the known values array, something like that, along these lines. Thank you...
  9. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 20 May 2013 Link to this post

    Hello Michael,

    Thank you for writing back.

    There is one major difference between using a TypeConverter and the approach with the custom editor from my previous post. If you are using a type converter it will create and assign a new instance of your class every time you edit the property. If you are using a custom editor you will be able to assign the object that is contained in the AvailableDogs collection to the AdoptedDog property. I have attached an image where I have tried to illustrate the result from the two editing approaches.

    I hope this will help. Do not hesitate to write back with further questions.

    Regards,
    Ivan Petrov
    the Telerik team
    RadChart for WinForms is obsolete. Now what?
  10. Muthu
    Muthu avatar
    8 posts
    Member since:
    Oct 2012

    Posted 12 Jul 2013 Link to this post

    Hi, I have a Grid with Parent Child relation. Child grid is having a 2 columns A, B. In this B column is a RadDropDownLis which loads value based on the selection of Column A. How to do this.
  11. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 17 Jul 2013 Link to this post

    Hi Muthu,

    Thank you for writing.

    I would like to ask you to keep threads focused on one topic. As your post is a direct violation of rule N.4 of the forum rules I would ask you to open a new thread and post your questions there.

    Regards,
    Ivan Petrov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready