How to bind to a property of the parent data context from a child grid

2 posts, 0 answers
  1. Mark
    Mark avatar
    2 posts
    Member since:
    Aug 2014

    Posted 02 Oct 2014 Link to this post

    I am attempting to bind a GridViewComboBoxColumn's item source found in a child grid to a property in the data context of the parent row. I believe I need to use a relative source and find an ancestor; however, I am not sure what the ancestor type should be. I was hoping there was something similar to a ListBoxItem type but I have not found one.

    Here is my Grid:
    <telerik:RadGridView Name="ParentGrid" ItemsSource="{Binding ParentItemSource}">
                    <telerik:GridViewComboBoxColumn Name="Parent Attribute"></telerik:GridViewComboBoxColumn>
                    <telerik:GridViewTableDefinition />
                        <telerik:RadGridView Name="ChildGrid" ItemsSource="{Binding Values}">
                            <telerik:GridViewComboBoxColumn Name="Parent Value" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ???}}, Path=ParentAttribute.Values}"></telerik:GridViewComboBoxColumn>

    Here are my classes:

    public class MyAttribute
           public string Name { get; set; }
           public MyAttribute ParentAttribute { get; set; }
           public ObservableCollection<MyValue> Values { get; set; }
       public class MyValue
           public string Name { get; set; }
           public string Value { get; set; }

    What I would like to see happen is when the user changes the ParentAttribute (through the Parent Attribute combo box column), the ItemsSource of the Parent Value column in the child grid will be bound to the ParentAttribute.Values field.

  2. Boris
    Boris avatar
    276 posts

    Posted 07 Oct 2014 Link to this post

    Hello Mark,

    A possible way to bind to your selected parent item from your child grid is to do something like the following:

    <telerik:GridViewComboBoxColumn Header="Parent Value"
                       ItemsSource="{Binding ElementName=ParentGrid, Path=SelectedItem.ParentAttribute.Values}">

    Please keep in mind that you will need to configure the GridViewComboBoxColumn to display it's values. For a more practical examples, you can check the "ComboBox Column" sdk example and our "ComboBoxColumn" demo.

    As a side note - Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples developing our SDK Samples Browser. You can also use it to review the examples.

    I hope this helps.

    Boris Penev

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top