在启用分组的 WPF 数据网格编辑中出现问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的代码是错误的,GroupStyle的绑定路径应该是“Department”,而不是“Name”。
将您的代码从: 更改
为:
它应该可以正常工作。
I think your code is wrong, the binding path of the GroupStyle should be "Department", not "Name".
Change your code from:
to this:
It should work fine.
这解决了我的问题,我已将 SortDescription 添加到 CollectionViewSource 中。
This Solves my problem,I have added SortDescription to CollectionViewSource.