在启用分组的 WPF 数据网格编辑中出现问题

发布于 2024-11-07 20:22:59 字数 2438 浏览 0 评论 0原文

Datagrid xaml 代码:

  <controls:DataGrid Name="dataGrid" AutoGenerateColumns="False" >
    <controls:DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                     </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </controls:DataGrid.GroupStyle>

        <controls:DataGrid.Columns>
            <controls:DataGridTextColumn Header="Student ID"  Width="90*" MinWidth="120" Binding="{Binding StudentId}"/>
            <controls:DataGridTextColumn Header="Student Name" Width="90*" MinWidth="120" Binding="{Binding Name}"/>
            <controls:DataGridTextColumn Header="Score" Width="100*" MinWidth="150" Binding="{Binding Score}"/>
        </controls:DataGrid.Columns>
    </controls:DataGrid>

这里是背后的代码:

    void LoadDatagrid()
    {
        List<Student> _studentList = new List<Student>();

        _studentList.Add(new Student()
        {
            StudentId = 1,
            Name = "Paul Henriot",
            Department = "IT",
            Score = 540
        });

        _studentList.Add(new Student()
        {
            StudentId = 2,
            Name = "John Doe",
            Department = "IT",
            Score = 620
        });

        _studentList.Add(new Student()
        {
            StudentId = 3,
            Name = "Aria Cruz",
            Department = "ME",
            Score = 840
        });


        _studentList.Add(new Student()
        {
            StudentId = 4,
            Name = "Yoshi Latimer",
            Department = "ME",
            Score = 450
        });



        CollectionViewSource viewSource = new CollectionViewSource();
        viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Department"));
        viewSource.Source = _studentList; ;
        dataGrid.ItemsSource = viewSource.View;
    }

  public class Student
    {
        public int StudentId{ get; set; }
        public string Name { get; set; }
        public string Department { get; set; }
        public int Score { get; set; }
    }

当我尝试编辑第一个部门中的分数或名称时,编辑该行后跳到下。

在这方面需要帮助。

Datagrid xaml code:

  <controls:DataGrid Name="dataGrid" AutoGenerateColumns="False" >
    <controls:DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                     </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </controls:DataGrid.GroupStyle>

        <controls:DataGrid.Columns>
            <controls:DataGridTextColumn Header="Student ID"  Width="90*" MinWidth="120" Binding="{Binding StudentId}"/>
            <controls:DataGridTextColumn Header="Student Name" Width="90*" MinWidth="120" Binding="{Binding Name}"/>
            <controls:DataGridTextColumn Header="Score" Width="100*" MinWidth="150" Binding="{Binding Score}"/>
        </controls:DataGrid.Columns>
    </controls:DataGrid>

Here the code behind:

    void LoadDatagrid()
    {
        List<Student> _studentList = new List<Student>();

        _studentList.Add(new Student()
        {
            StudentId = 1,
            Name = "Paul Henriot",
            Department = "IT",
            Score = 540
        });

        _studentList.Add(new Student()
        {
            StudentId = 2,
            Name = "John Doe",
            Department = "IT",
            Score = 620
        });

        _studentList.Add(new Student()
        {
            StudentId = 3,
            Name = "Aria Cruz",
            Department = "ME",
            Score = 840
        });


        _studentList.Add(new Student()
        {
            StudentId = 4,
            Name = "Yoshi Latimer",
            Department = "ME",
            Score = 450
        });



        CollectionViewSource viewSource = new CollectionViewSource();
        viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Department"));
        viewSource.Source = _studentList; ;
        dataGrid.ItemsSource = viewSource.View;
    }

  public class Student
    {
        public int StudentId{ get; set; }
        public string Name { get; set; }
        public string Department { get; set; }
        public int Score { get; set; }
    }

when i am trying to edit the score or name in first any one of the department,after editing that row jumps to down.

Need a help on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-11-14 20:22:59

我认为你的代码是错误的,GroupStyle的绑定路径应该是“Department”,而不是“Name”。

将您的代码从: 更改

        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>

为:

        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Department}" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>

它应该可以正常工作。

I think your code is wrong, the binding path of the GroupStyle should be "Department", not "Name".

Change your code from:

        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>

to this:

        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Department}" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>

It should work fine.

初熏 2024-11-14 20:22:59

这解决了我的问题,我已将 SortDescription 添加到 CollectionViewSource 中。

        CollectionViewSource viewSource = new CollectionViewSource();
        viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Department"));
        viewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("Department", System.ComponentModel.ListSortDirection.Ascending));
        viewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("StudentId", System.ComponentModel.ListSortDirection.Ascending));
        viewSource.Source = _studentList; ;
        dataGrid.ItemsSource = viewSource.View;

This Solves my problem,I have added SortDescription to CollectionViewSource.

        CollectionViewSource viewSource = new CollectionViewSource();
        viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Department"));
        viewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("Department", System.ComponentModel.ListSortDirection.Ascending));
        viewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("StudentId", System.ComponentModel.ListSortDirection.Ascending));
        viewSource.Source = _studentList; ;
        dataGrid.ItemsSource = viewSource.View;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文