How to bind gridview cell button in gridview hierarchy?

1 Answer 56 Views
GridView
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Psyduck asked on 07 Oct 2021, 09:08 AM | edited on 07 Oct 2021, 09:11 AM

Hello.

Inside the gridview is a hierarchical gridview.

I created a button of the column cell template in the hierarchical grid view.

How to bind this button and how to receive parameters?

Upload a simple example image and source.


<telerik:RadGridView x:Name="xMain"  ItemsSource="{Binding Main}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Name"  DataMemberBinding="{Binding Name}"/>
        <telerik:GridViewColumn Header="View" Width="80" HeaderTextAlignment="Center" TextAlignment="Center">
            <telerik:GridViewColumn.CellTemplate>
                <DataTemplate>
                    <telerik:RadButton	Content="MainButton"
										Command="{Binding DataContext.OnClickCommand, RelativeSource={RelativeSource FindAncestor,AncestorType=telerik:RadGridView}}"
										CommandParameter="{Binding}">
                    </telerik:RadButton>
                </DataTemplate>
            </telerik:GridViewColumn.CellTemplate>
        </telerik:GridViewColumn>

        <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
            <telerik:GridViewTableDefinition.Relation>
                <telerik:PropertyRelation ParentPropertyName="Subs" />
            </telerik:GridViewTableDefinition.Relation>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>

    <telerik:RadGridView.HierarchyChildTemplate>
        <DataTemplate>
                <telerik:RadGridView x:Name="xSubs" ItemsSource="{Binding Subs}">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="Name"  DataMemberBinding="{Binding Name}"/>
                    <telerik:GridViewColumn Header="View" Width="80" HeaderTextAlignment="Center" TextAlignment="Center">
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <telerik:RadButton	Content="SubButton"
													Command="{Binding DataContext.OnClickCommand, RelativeSource={RelativeSource FindAncestor,AncestorType=telerik:RadGridView}}"
													CommandParameter="{Binding}">
                                </telerik:RadButton>
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </DataTemplate>
    </telerik:RadGridView.HierarchyChildTemplate>
</telerik:RadGridView>

It is bound to the same OnClickCommand and will receive an object to identify it.

(CommandParameter is the selecteditem that is bound to the GridView.)

 

MainButton working / SubButton not working

I also tried setting Mode=TemplatedParent of RelativeSource and other settings, but couldn't find a solution.

I will wait for your reply.
thank you.

1 Answer, 1 is accepted

Sort by
1
Accepted
Stenly
Telerik team
answered on 12 Oct 2021, 08:44 AM

Hello Kim Jusung,

In order to set the command to work for the button in the hierarchy, you need to set the binding path to the ParentRow. The ParentRow exposes the GridViewDataControl property, which is the parent GridView, and its own data context can be accessed through the DataContext property. The following code snippet shows how to bind the child button to the command present in the view model:

<DataTemplate>
    <telerik:RadButton Content="SubButton"
				Command="{Binding ParentRow.GridViewDataControl.DataContext.OnClickCommand, RelativeSource={RelativeSource FindAncestor,AncestorType=telerik:RadGridView}}" CommandParameter="{Binding}">
    </telerik:RadButton>
</DataTemplate>

Alternatively, you could set the view model as a resource and access the command using the StaticResource extension when binding.

That said, I have prepared a sample project implementing the first approach. Please have a look and let me know if this also works in your original application.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
commented on 13 Oct 2021, 06:04 AM

Thanks it actually works fine for my project as well.

For binding of "ParentRow.GridViewDataControl", is there any documentation I can check?
Your ParentRow link couldn't find a usage example.
Stenly
Telerik team
commented on 14 Oct 2021, 05:16 PM

The ParentRow property does not have any dedicated documentation as it is a property that only exposes the parent row when a hierarchy is used. However, you can still look at each property and its description in the provided API reference section. 
Tags
GridView
Asked by
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Answers by
Stenly
Telerik team
Share this question
or