C#WPF DataGrid以编程方式设置多个选定项
我有一个WPF窗口C#WPF DataGrid以编程方式设置多个选定项,c#,wpf,linq,xaml,datagrid,C#,Wpf,Linq,Xaml,Datagrid,我有一个WPF窗口ViewAssignedStudentsWindow,具有以下构造函数: public ViewAssignedStudentsWindow(IEnumerable<StudentDTO> allStudents, MandatoryLessonDTO lesson) { InitializeComponent(); studentsGrid.ItemsSource = allStudents; studentsGrid.SelectedIt
ViewAssignedStudentsWindow
,具有以下构造函数:
public ViewAssignedStudentsWindow(IEnumerable<StudentDTO> allStudents, MandatoryLessonDTO lesson)
{
InitializeComponent();
studentsGrid.ItemsSource = allStudents;
studentsGrid.SelectedItems.Add(allStudents.Where(x => lesson.StudentIds.Contains(x.Id)));
}
问题是网格中充满了学生的数据,但是
SelectedItems
似乎不起作用-没有选择任何项目。如果我尝试调试代码,LINQ似乎会产生正确的结果,但是SelectedItems
保持为空。在这一点上我完全迷失了为什么这不起作用,这似乎是一项如此简单的任务。任何帮助都将不胜感激。我建议将数据绑定到此处的Items属性
你们班的学生:
public class Students : INotifyPropertyChanged {
private bool _selected;
public bool Selected {
get { return _selected; }
set {
if (value == _selected) return;
_selected = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
将行样式添加到DG:
<DataGrid ... >
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected" Value="{Binding Selected}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
从CodeBehind中选择行:
public ViewAssignedStudentsWindow(IEnumerable<StudentDTO> allStudents, MandatoryLessonDTO lesson)
{
InitializeComponent();
studentsGrid.ItemsSource = allStudents;
allStudents.ForEach(x => x.Selected = lesson.StudentIds.Contains(x.Id)));
}
public view assigned studentswindow(IEnumerable all students,MandatoryLessonDTO lesson)
{
初始化组件();
studentsGrid.ItemsSource=所有学生;
allStudents.ForEach(x=>x.Selected=lesson.studentId.Contains(x.Id));
}
我建议对此处的Items属性使用数据绑定
你们班的学生:
public class Students : INotifyPropertyChanged {
private bool _selected;
public bool Selected {
get { return _selected; }
set {
if (value == _selected) return;
_selected = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
将行样式添加到DG:
<DataGrid ... >
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected" Value="{Binding Selected}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
从CodeBehind中选择行:
public ViewAssignedStudentsWindow(IEnumerable<StudentDTO> allStudents, MandatoryLessonDTO lesson)
{
InitializeComponent();
studentsGrid.ItemsSource = allStudents;
allStudents.ForEach(x => x.Selected = lesson.StudentIds.Contains(x.Id)));
}
public view assigned studentswindow(IEnumerable all students,MandatoryLessonDTO lesson)
{
初始化组件();
studentsGrid.ItemsSource=所有学生;
allStudents.ForEach(x=>x.Selected=lesson.studentId.Contains(x.Id));
}