views:

1423

answers:

2

Hi,

My WPF page has a RadGrid control provided by Telerik. The Grid is a nested grid which essentially means that clicking on the (+) sign on the leftmost column of the row expands the row into a Subgrid. This is being done by specifying a hierarchical grid in my XAML. Everything works just fine when you click on the row and expand the subgrid but the selectedItem of the initially selected row does not seem to change. An example would be selecting row 1 of the grid initially and then expanding row 4 to display the subgrid. The subgrid is displayed but the selectedItem is still row 1. The desired behavior is for row 4 to be the selectedItem once it is expanded to display the subgrid. Can anyone point out what exactly is going wrong over here.

Thanks

A: 

Here is an example:

XAML

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"    
Title="Window1">
<Grid>
    <telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding}">
        <telerik:RadGridView.ChildTableDefinitions>
            <telerik:GridViewTableDefinition  />
        </telerik:RadGridView.ChildTableDefinitions>
        <telerik:RadGridView.HierarchyChildTemplate>
            <DataTemplate>
                <telerik:RadGridView ItemsSource="{Binding Items}" Loaded="RadGridView_Loaded" />
            </DataTemplate>
        </telerik:RadGridView.HierarchyChildTemplate>
    </telerik:RadGridView>
</Grid>

C#

using System.Windows;

using System.Linq;

namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent();

        DataContext = from i in Enumerable.Range(0, 10)
                      select new
                      {
                          ID = i,
                          Items = from j in Enumerable.Range(0, 10)
                                  select new
                                  {
                                      ID = j,
                                  }
                      };
    }

    private void RadGridView_Loaded(object sender, RoutedEventArgs e)
    {
        RadGridView1.SelectedItem = ((FrameworkElement)sender).DataContext;
    }
}

}

Vlad
Thanks Vlad but the Loaded event fires only the first time the grid is loaded so clicking on the (+) sign for the first time calls the RadGridView_Loaded, subsequent clicks on the same row does not invoke the event. Is there some other event needs to be associated with either the parent or the child grid?
sc_ray
+1  A: 

Your are right - here is the updated version:

    private void RadGridView_Loaded(object sender, RoutedEventArgs e)
    {
        var childGrid = (RadGridView)sender;
        var parentRow = childGrid.ParentRow;

        if (parentRow != null)
        {
            RadGridView1.SelectedItem = childGrid.DataContext;
            parentRow.IsExpandedChanged += new RoutedEventHandler(parentRow_IsExpandedChanged);
        }
    }

    void parentRow_IsExpandedChanged(object sender, RoutedEventArgs e)
    {
        RadGridView1.SelectedItem = ((GridViewRow)sender).DataContext;
    }
Vlad
Thanks Vlad. The logic does look like something that will solve the problem. But seems like the IsExpandedChanged event is not available for the parentRow. Has it been added to a later version of Telerik?
sc_ray
Indeed this event is added in Q3 2009 SP1 - more info here: http://www.telerik.com/products/wpf/whats-new/release-history/q3-2009-sp1-version-2009-3-1208-183011105.aspx
Vlad
Thanks Vlad. This helps.
sc_ray