Problems with binding GridViewComboBoxColumn to a dynamic property

8 posts, 1 answers
  1. Angerico
    Angerico avatar
    4 posts
    Member since:
    Mar 2012

    Posted 03 Apr 2012 Link to this post

    I've been using dynamic objects bound to a RadGridView control. Yeah our business model here is quite weird. Anyway, they've been working fine and dandy... until I tried implementing a combobox column! *insert ominous sound here*

    Here's how to reproduce the problem:

    Define a LookupItem class as:
    public class LookupItem
    {
     public object Key { get; set; }
     public object Value { get; set; }
    }

    Create instances of this class and put them into a list called lookups that you will use later as an ItemsSource for a GridViewComboBoxColumn.
    var lookups = new ObservableCollection<LookupItem>();
    lookups.Add(new LookupItem() { Key = "1", Value = "test1 - value" });
    lookups.Add(new LookupItem() { Key = "2", Value = "test2 - value" });

    Afterwards, create a collection of dynamic objects using the ExpandoObject class which you will then put into a grid called "gridView".
    var list = new ObservableCollection<ExpandoObject>();
    dynamic row = new ExpandoObject();
    row.Prop1 = "1";
    row.Prop2 = "foo";
    list.Add(row);
    row = new ExpandoObject();
    row.Prop1 = "2";
    row.Prop2 = "bar";
    list.Add(row);
    gridView.ItemsSource = list;

    Finally, we manually specify the columns for the grid. Don't forget to assign the lookups we created to the combobox column's ItemsSource.
    gridView.Columns.Clear();
    var column1 = new GridViewComboBoxColumn();
    column1.DisplayMemberPath = "Value";
    column1.SelectedValueMemberPath = "Key";
    column1.DataMemberBinding = new Binding("Prop1");
    column1.UniqueName = "Prop1";
    column1.ItemsSource = lookups;
    gridView.Columns.Add(column1);
     
    var column2 = new GridViewDataColumn();
    column2.DataMemberBinding = new Binding("Prop2");
    column2.UniqueName = "Prop2";
    gridView.Columns.Add(column2);

    If you try that, you will get a grid with two columns, one with a combobox that is not correctly bound, and one with normal text that is correctly bound to "Prop2".

    And if you do not use dynamic classes, i.e. you specifically create "Prop1" and "Prop2" properties, you will see that the combobox column will be bound correctly to "Prop1".

    Is this a limitation on the current control, or is there a possible workaround? Looking forward to the fun times ahead...
  2. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 03 Apr 2012 Link to this post

    Hello,

    Silverlight does not perform well in the context of dynamic objects and bindings. The combo column internally uses bindings for the lookup logic so I am afraid the combo column can not be used in such setup.

    There is a possible workaround. If we drop the combo column and use a regular column with a combo box in the cell template we ay be able to overcome this limitation.

    Let me know in case this works for you and I will gather  a small sample for you .

    Regards,
    Pavel Pavlov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Angerico
    Angerico avatar
    4 posts
    Member since:
    Mar 2012

    Posted 04 Apr 2012 Link to this post

    Thanks for the quick reply. I'll try that and get back to you if there is an issue.

    Just a quick question: if I mark your reply as "The Answer" but I suddenly want additional information, will I still be able to reply to this thread? Or will it become closed automatically on account of it being answered already?
  4. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 Apr 2012 Link to this post

    Hello,

     You can always reopen this thread. 

    Regards,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Angerico
    Angerico avatar
    4 posts
    Member since:
    Mar 2012

    Posted 04 Apr 2012 Link to this post

    I think I'm going to invoke that help now. I've been trying to bind a combobox but it seems to be tricky to do so if it's in a DataTemplate. I don't want to bind it to a property in the objects in the grid and I need to do it via code and not thru a StaticResource (because of the dynamic nature of our business rules here). I can use the XamlReader to do this, but I don't know what to assign to the combobox's ItemsSource.

    I hope I made sense.
  6. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 Apr 2012 Link to this post

    Hi,

    If you do not want this combo to be related to something from the grid source how do you bind it outside of the grid? 

    Kind regards,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  7. Angerico
    Angerico avatar
    4 posts
    Member since:
    Mar 2012

    Posted 04 Apr 2012 Link to this post

    Hi Vlad,

    Sorry you must've misunderstood me. The combo box is still related to the grid via the combo box's Selected Value and a property in the current row. Using my simple example above, I have a list of LookupItems, each having properties Key and Value. I have a grid of objects with properties called Prop1 and Prop2. I need to equate the Key property of the LookupItems to the Prop1 property of the grid objects.

    I originally used a GridViewComboBoxColumn but it does not play nicely if Prop1 happened to be a dynamic property. If it was a static property, it works fine.

    So now I want to try out the suggestion of using a simple column but with a combo box for its CellEditTemplate.
  8. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 04 Apr 2012 Link to this post

    Hi Angerico,

    What do you mean by the "dynamic nature of business rules"  ? I ask you because using a static resource seems the only descent  option here.  Also the static resource does not prevent you form dynamically changing a property of itself. Another clarification may be needed as well - do all the combos ( in all rows) share the same ItemsSource or it will be individual per row ? .

    Kind regards,
    Pavel Pavlov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top