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

RadGridView MVVM Bind Row Expanded

3 Answers 469 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 10 Mar 2014, 06:19 PM
I have a WPF /MVVM application. I need to put  2 buttons, Expand All and Collapse All above the grid. I need an example of binding each row's expanded property to a view model.

Thank you

3 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 10 Mar 2014, 06:36 PM
Hi Kevin,

Generally, you can use GridView's ExpandAllHierarchyItems and CollapseAllHierarchyItems methods. As for binding of GridViewRow's IsExpanded - you can bind it to a property of your business object by subscribing to GridView's RowLoaded event. It will be something similar to this:
private void clubsGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
            var row = e.Row as GridViewRow;
 
            if (row == null)
                return;
 
            var selectedBinding = new Binding("IsExpanded");
            selectedBinding.Source = row.Item;
            selectedBinding.Mode = BindingMode.TwoWay;
 
            row.SetBinding(GridViewRow.IsExpandedProperty, selectedBinding);
        }


Regards,
Yoan
Telerik

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

0
Kevin
Top achievements
Rank 1
answered on 10 Mar 2014, 06:41 PM
Subscribing to an event means I now have data code in the code behind for the view, which is not MVVM. Is there not a row template with an IsExpanded property that I can bind to?
0
Yoan
Telerik team
answered on 10 Mar 2014, 07:25 PM
Hi Kevin,

If you want to have your code-behind clean, the way to go is with an Attached behavior. Here you can find information about it. You will write the attached behavior only once. This attached behavior will attach to the event and will set the binding. Then you can enable this attached behavior on a RadGridView. Here is a very nice article about event handling with attached behaviors. You have the code in one place only. All you will need to do is something like this:

<RadGridView EnableMyAttachedBehavior="True"/> and then let the attached behavior kick in and do its job. 


Regards,
Yoan
Telerik

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

Tags
GridView
Asked by
Kevin
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Kevin
Top achievements
Rank 1
Share this question
or