这是我的问题:我有一个包含学生的清单框。当通过选中复选框选择学生时,我希望它显示在数据网格中。此外,如果学生未选中,则应将其从数据网格中删除。
我可以将我的集合绑定到一个清单框,但是当一个学生被选中时,它不会触发我假设的属性更改事件,因为更改的属性是它绑定到的集合的子项。所以我也将属性更改为集合。
下面是我一直在玩的一些代码。
绑定到哪里:
public IEnumerable<SelectedStudent> ActiveStudents
{
get { return _activeStudents; }
set
{
_activeStudents = value;
RaisePropertyChanged("ActiveStudents");
RaisePropertyChanged("GridDisplay");
}
}
public IEnumerable<SelectedStudent> GridDisplay
{
get
{
var aa = _activeStudents.Where(a => a.Select == true);
return aa;
}
set
{
_activeStudents = value;
RaisePropertyChanged("GridDisplay");
}
}
选定的学生班级:
#region Fields
private int _studentId;
private string _firstName;
private string _lastName;
private bool _printCertificate;
private bool _select;
private int _testDateId;
private string _rank;
#endregion
public int StudentId
{
get { return _studentId; }
set
{
_studentId = value;
RaisePropertyChanged("StudentId");
}
}
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
RaisePropertyChanged("FirstName");
}
}
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
RaisePropertyChanged("LastName");
}
}
public string Student
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
public bool PrintCertificate
{
get { return _printCertificate; }
set
{
_printCertificate = value;
RaisePropertyChanged("PrintCertificate");
}
}
public bool Select
{
get { return _select; }
set
{
_select = value;
RaisePropertyChanged("Select");
}
}
public int TestDateId
{
get { return _testDateId; }
set
{
_testDateId = value;
RaisePropertyChanged("TestDateId");
}
}
public string Rank
{
get { return _rank; }
set
{
_rank = value;
RaisePropertyChanged("Rank");
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#endregion
现在我的 Xaml
<xctk:CheckListBox Height="200"
ItemsSource="{Binding Path=ActiveStudents, Mode=TwoWay}"
Name="ActiveStudents"
DisplayMemberPath="Student"
ValueMemberPath="StudentID"
SelectedMemberPath="Select"
SelectedItem="{Binding SelectedStudent}"
SelectedItemsOverride="{Binding SelectedStudentValue}"/>
<DataGrid MaxHeight="200" MinHeight="200" Name="MyGrid"
Grid.Row="1" Grid.Column="1"
VerticalAlignment="Top"
HeadersVisibility="Column"
AutoGenerateColumns="False"
AlternatingRowBackground="#F0EDF2"
ItemsSource="{Binding GridDisplay}"
VerticalScrollBarVisibility="Auto"
CanUserAddRows="false">
<DataGrid.GroupStyle>.....
最重要的是,我有一个下拉列表,用于决定在复选框中预先检查哪些学生,从而使他们显示在数据网格中。最后一部分我确信我可以弄清楚,但我无法让我从复选框中选择的学生显示在数据网格中。
任何帮助,将不胜感激。