c# – 分层数据网格子项

<telerik:RadGridView x:Name="rgvData" CanUserFreezeColumns="False" GridLinesVisibility="Horizontal" IsReadOnly="False" AutoGenerateColumns="False"
                         telerik:Theming.Theme="Office_Blue" ItemsSource="{Binding}" >
        <telerik:RadGridView.ChildTableDefinitions>
            <telerik:GridViewTableDefinition />
        </telerik:RadGridView.ChildTableDefinitions>
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding StudentID}" Header="Category ID" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding StudentFN}" Header="Category Name" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding StudentLN}" Header="Description" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Picture}" Header="Picture" />
        </telerik:RadGridView.Columns>
        <telerik:RadGridView.HierarchyChildTemplate>
            <DataTemplate>
                <telerik:RadGridView x:Name="RadGridView1" Loaded="rgvData_Loaded" ShowInsertRow="True"  CanUserInsertRows="True"  ItemsSource="{Binding Subjects}" CanUserFreezeColumns="False" AutoGenerateColumns="False" ShowGroupPanel="False" IsReadOnly="False">
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding SubjectID}" Header="Order ID" />
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding SubjectName}" Header="Employee ID" />
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding SubjectProfessor}" Header="Order Date" />
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding ShipCountry}" Header="Ship Country" />
                    </telerik:RadGridView.Columns>
                </telerik:RadGridView>
            </DataTemplate>
        </telerik:RadGridView.HierarchyChildTemplate>
    </telerik:RadGridView>

MainXaml.Cs

public partial class MainWindow : Window
{
    DataAccessDataContext m_dcData;
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        this.rgvData.Items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_CollectionChanged);
        rgvData.Filtered += new EventHandler<Telerik.Windows.Controls.GridView.GridViewFilteredEventArgs>(rgvData_Filtered);
    }

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

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

void parentRow_IsExpandedChanged(object sender, RoutedEventArgs e)
{
    rgvData.SelectedItem = ((GridViewRow)sender).DataContext;
}x


    void rgvData_Filtered(object sender, Telerik.Windows.Controls.GridView.GridViewFilteredEventArgs e)
    {
        DataContext = m_dcData;
        m_dcData = new DataAccessDataContext();
        m_dcData.Log = Console.Out;
        rgvData.ItemsSource = m_dcData.Students;
        rgvData.ShowInsertRow = true;
    }

    private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        try
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (Student item in e.NewItems)
                {
                    m_dcData.Students.InsertOnSubmit(item);
                }
            }
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach (Student item in e.OldItems)
                {
                    m_dcData.Students.DeleteOnSubmit(item);
                }
            }
        }
        catch (Exception s)
        { }
    }
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        m_dcData = new DataAccessDataContext();
        m_dcData.Log = Console.Out;
        rgvData.ItemsSource = m_dcData.Students;
        rgvData.ShowInsertRow = true;

        rgvData.ItemsSource = m_dcData.Students;
        rgvData.ShowInsertRow = true;
    }
}

嗨我加载与其父源相关的子数据没有问题.问题是如果我尝试扩展另一行,它会为它加载第一个数据.例如,如果我展开了row1,它会显示正确的子表,但是如果我转到row2并展开它,它的子记录是针对row1的.我尝试寻找答案并将parent_IsExpandedChanged事件处理它,但它仍然不会显示正确的数据.如果我再次尝试重新加载应用程序并尝试扩展第2行或第3行.它的子项是正确的但如果我展开另一行它将显示第2行或第3行的子项.请帮助我做错了什么

解决方法:

您的XAML看起来很健康,我看不到您发布的代码会导致问题.您确认加载的数据是否正确?您实际上可能正在为每个学生加载相同的主题数据.
您可以通过将此代码放入窗口上的按钮单击事件来轻松检查:

IEnumerable<Student> list = (IEnumerable<Student>)this.DataContext;
foreach(Student stu in Students)
{
    Debug.WriteLine(stu.StudentID + ":");
    foreach(Subject sub in stu.Subjects)
    {
        Debug.WriteLine("\\t" + sub.SubjectID)
    }
}
上一篇:c# – FindVisualChild参考问题


下一篇:如何在C#Windows窗体应用程序中搜索数据gridview?