This is a migrated thread and some comments may be shown as answers.

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

1 Answer 2937 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 02 Oct 2014, 03:52 PM
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:RadGridView.Columns>
                <telerik:GridViewComboBoxColumn Name="Parent Attribute"></telerik:GridViewComboBoxColumn>
            </telerik:RadGridView.Columns>
            <telerik:RadGridView.ChildTableDefinitions>
                <telerik:GridViewTableDefinition />
            </telerik:RadGridView.ChildTableDefinitions>
            <telerik:RadGridView.HierarchyChildTemplate>
                <DataTemplate>
                    <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>
                    </telerik:RadGridView>
                </DataTemplate>
            </telerik:RadGridView.HierarchyChildTemplate>
        </telerik:RadGridView>

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.


 

1 Answer, 1 is accepted

Sort by
0
Boris
Telerik team
answered on 07 Oct 2014, 08:54 AM
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.

Regards,
Boris Penev
Telerik
 

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.

 
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Boris
Telerik team
Share this question
or