Telerik Radgrid WPF

时间:2010-01-20 12:07:18

标签: wpf xaml gridview telerik selecteditem

我的WPF页面有Telerik提供的RadGrid控件。 Grid是一个嵌套网格,实质上意味着单击该行最左侧列上的(+)符号会将该行扩展为Subgrid。这是通过在我的XAML中指定分层网格来完成的。当您单击行并展开子网格时,一切正常,但最初选定行的selectedItem似乎没有变化。一个示例是最初选择网格的第1行,然后展开第4行以显示子网格。显示子网格,但selectedItem仍然是第1行。一旦展开显示子网格,第4行将成为selectedItem的所需行为。任何人都可以指出这里到底出了什么问题。

谢谢

2 个答案:

答案 0 :(得分:1)

你说得对 - 这是更新版本:

    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;
    }

答案 1 :(得分:0)

以下是一个例子:

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;
        }
    }
}