我将这种方法用于类似的应用程序。一个单独的列用于选择一个人。该列的标题中还有一个复选框,可以选择/取消选择所有人。
视图模型
private ObservableCollection<PersonModel> _peopleCollection;
public ObservableCollection<PersonModel> PeopleCollection
{
get { return _peopleCollection; }
set
{
_peopleCollection = value;
RaisePropertyChanged("PeopleCollection");
}
}
private void initPeople()
{
foreach (var person in PeopleCollection)
{
person.PropertyChanged += PersonOnPropertyChanged;
}
}
private void PersonOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
if(propertyChangedEventArgs.PropertyName == "IsPersonSelected")
{
//Handle checked/unchecked person
}
}
private bool _allPersonsSelected
public bool AllPersonsSelected
{
get { return _allPersonsSelected; }
set
{
_allPersonsSelected= value;
RaisePropertyChanged("AllPersonsSelected");
if (value)
SelectRows();
else
{
DeselectRows();
}
}
}
public void SelectRows()
{
foreach (var record in PeopleCollection)
record.IsPersonSelected = true;
}
public void DeselectRows()
{
foreach (var record in PeopleCollection)
record.IsPersonSelected = false;
}
}
这是视图中的网格 xaml
<dxg:GridControl ItemsSource="{Binding Path=PeopleCollection}">
<dxg:GridControl.View>
<dxg:TableView>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="IsPersonSelected"
HorizontalHeaderContentAlignment="Left">
<dxg:GridColumn.Header>
<StackPanel Orientation="Horizontal">
<TextBlock>Selected</TextBlock>
<dxe:CheckEdit EditValue="{Binding Path=AllPersonsSelected}"
Margin="10,0,0,0" />
</StackPanel>
</dxg:GridColumn.Header>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Name" Header="Name"/>
<dxg:GridColumn FieldName="Age" Header="Age"/>
</dxg:GridControl.Columns>
</dxg:GridControl>