RadGridView MVVM Bind Row Expanded

4 posts, 0 answers
  1. Kevin
    Kevin avatar
    17 posts
    Member since:
    Feb 2011

    Posted 10 Mar 2014 Link to this post

    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
  2. Yoan
    Admin
    Yoan avatar
    1215 posts

    Posted 10 Mar 2014 Link to this post

    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.

  3. Kevin
    Kevin avatar
    17 posts
    Member since:
    Feb 2011

    Posted 10 Mar 2014 in reply to Yoan Link to this post

    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?
  4. Yoan
    Admin
    Yoan avatar
    1215 posts

    Posted 10 Mar 2014 Link to this post

    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.

Back to Top