<DataGrid Name="dataGrid" IsReadOnly="False" Width="600" Height="400" AutoGenerateColumns="False" ItemsSource="{Binding GeneryModelList}" > <DataGrid.Columns> <DataGridTemplateColumn Width="150"> <DataGridTemplateColumn.Header> <CheckBox Command="{ Binding DataContext.SelectOrUnSelectAll,RelativeSource={RelativeSource AncestorType=DataGrid}}" Content="全选"></CheckBox> </DataGridTemplateColumn.Header> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></CheckBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
开始原因是由于并不知道使用数据模板,需要加入数据源,要找到数据源的作用域。后面的命令部分代码如下:
public ICommand SelectOrUnSelectAll { get { return new GeneryModelSoft.Commands.Command(p => SelectAll()); } }
protected void SelectAll() {
if (generyModelList != null&&isSelectFlag==false) { for (int i = 0; i < generyModelList.Count; i++) { generyModelList.IsSelected = true; } isSelectFlag = true; } else if (generyModelList != null && isSelectFlag == true) { for (int i = 0; i < generyModelList.Count; i++) { generyModelList.IsSelected = false; } isSelectFlag = false; }
}
|