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

Hierachical Grid using XAML rather than code behind

3 Answers 216 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 09 May 2011, 12:18 AM
I have implemented a hierarchical grid based on the example provided on the Telerik site. This works fine.

However, I am having difficulties binding to properties in my viewmodel from the code behind and the example given seems to bind only to objects in the top level RadGridView's ItemsSource. Although I can continue working in the code behind I would like to see an example of not using the DataLoading eventhandler and sticking with XAML to define the columns for the child rows.

I am unable to find any example of placing the   

 

<telerik:RadGridView.Columns>

 

or its equivalent section within the ChildTableDefinitions. Surely this must be possible, as these child rows in my case, anyway, contain merely a subset of the columns in the main RadGridView control.

So basically, what I am asking is to be able to do something like the following, where I can handle the table's columns in XAML...

<telerik:RadGridView.ChildTableDefinitions>
    <telerik:GridViewTableDefinition>
        <telerik:GridViewTableDefinition.Relation>
            <telerik:TableRelation IsSelfReference="True">
                <telerik:TableRelation.FieldNames>
                    <telerik:FieldDescriptorNamePair 
                ParentFieldDescriptorName="BankStatementTranID" 
                ChildFieldDescriptorName="ParentStatementTranID"/>
                </telerik:TableRelation.FieldNames>
            </telerik:TableRelation>
        </telerik:GridViewTableDefinition.Relation>
  
        <telerik:RadGridView.Columns>
         ...My XAML column code here...
        </telerik:RadGridView.Columns>
    </telerik:GridViewTableDefinition>
</telerik:RadGridView.ChildTableDefinitions>

 

 

 

 

 

 

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Rob
Top achievements
Rank 1
answered on 09 May 2011, 04:37 AM
Having looked into this further, I have continued down the code behind method for now to get things running. This is achieved by replacing my previous XAML bindings with an instance of my viewmodel and setting this.DataContext to it in grid1_DataLoading event.

GridViewDataControl dataControl = (GridViewDataControl)sender;
            if (dataControl.ParentRow != null)
            {
                CbBankRecViewModel vm = this.DataContext as CbBankRecViewModel;

and it is referenced simply as...
combo = new GridViewComboBoxColumn();
combo.Header = "Branch";
combo.DataMemberBinding = new Binding("BranchID");
combo.ItemsSource = vm.Branches;
combo.DisplayMemberPath = "Description";
combo.SelectedValueMemberPath = "LookupID";
combo.IsVisible = vm.CurrentApplicationSettingContext.Financials_BranchesActive;
dataControl.Columns.Add(combo);


For those interested in this thread or also hunting around for ways to do the heirarchical grid in XAML, I almost had the following construct working.
<telerik:RadGridView.ChildTableDefinitions>
     <telerik:GridViewTableDefinition />
</telerik:RadGridView.ChildTableDefinitions>
  
<telerik:RadGridView.HierarchyChildTemplate>
<DataTemplate>
<telerik:RadGridView x:Name="RadGridView1" CanUserFreezeColumns="False" AutoGenerateColumns="False" ...>
<telerik:RadGridView.Columns>
...my column XAML here...
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</DataTemplate>
</telerik:RadGridView.HierarchyChildTemplate>

but somehow I could not quite get this working. Perhaps somone not new to Telerik controls will be able to take this further and get it working properly.

Hope this helps someone.
0
Accepted
Maya
Telerik team
answered on 09 May 2011, 07:41 AM
Hi Rob,

Indeed, if you want to define the columns for the child grid, you can follow the example into our online documentation:

<telerik:RadGridView Name="gridView" ItemsSource="{Binding Person}">
   <telerik:RadGridView.ChildTableDefinitions>
               <telerik:GridViewTableDefinition />
   </telerik:RadGridView.ChildTableDefinitions>
   <telerik:RadGridView.HierarchyChildTemplate>
       <DataTemplate>
           <telerik:RadGridView ItemsSource="{Binding Children}" Name="childGrid" ShowGroupPanel="False" />
       </DataTemplate>
   </telerik:RadGridView.HierarchyChildTemplate>
</telerik:RadGridView>
 
Still, I am sending you a sample project illustrating the approach.


Regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rob
Top achievements
Rank 1
answered on 09 May 2011, 09:05 PM
Thankyou Maya. It turns out all that was wrong was my binding for ItemsSource on the RadGridView nested within the HierarchyChildTemplate was incorrect. The DataContext moves down to the individual object/row of the parent automatically which I somehow missed. So I was trying to bind from one level too high.

The child columns are now showing and can be customised within the XAML rather than code-behind which is preferable in this current project. Great stuff!

Thanks,
Rob
Tags
GridView
Asked by
Rob
Top achievements
Rank 1
Answers by
Rob
Top achievements
Rank 1
Maya
Telerik team
Share this question
or