成品图:
xaml代码
<Grid>
<DataGrid x:Name="datagrid" Height="Auto" Width="Auto" Grid.Row="1"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False" >
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="GotFocus" Handler="Item_GotFocus"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="全选" Binding="{Binding IsSelected,Mode=TwoWay}">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="cbCheck" IsChecked="{Binding ElementName=root,Path=CheckboxIsSelected}" Content="选择" Width="60" Click="cbCheck_Click"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="名字" Binding="{Binding Name}"/>
<DataGridTextColumn Header="性别" Binding="{Binding Sex}"/>
<DataGridTextColumn Header="年龄" Binding="{Binding Age}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
cs代码
/// <summary>
/// 设置数据源
/// </summary>
void SetItemsSource()
{
List<Person> list = new List<Person>();
for (int i = 0; i < 10; i++)
{
list.Add(new Person() { Name = "" + i, Age = "1" + i, Sex = i % 3 == 0 ? "男" : "女" });
}
datagrid.ItemsSource = list;
}
int selectedCount = 0;
/// <summary>
/// 绑定列头的勾选框
/// </summary>
public bool? CheckboxIsSelected
{
get { return (bool?)GetValue(CheckboxIsSelectedProperty); }
set { SetValue(CheckboxIsSelectedProperty, value); }
}
public static readonly DependencyProperty CheckboxIsSelectedProperty =
DependencyProperty.Register("CheckboxIsSelected", typeof(bool?), typeof(MainWindow), new PropertyMetadata(false));
/// <summary>
/// 选中列自动勾选勾选框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Item_GotFocus(object sender, RoutedEventArgs e)
{
var item = (DataGridRow)sender;
var ss = item.Item as Person;
if (ss != null)
{
ss.IsSelected = !ss.IsSelected;
selectedCount += ss.IsSelected ? 1 : -1;
//if(ss.IsSelected)
//{
// ss.IsSelected = false;
// selectedCount--;
//}
//else
//{
// ss.IsSelected = true;
// selectedCount++;
//}
}
CheckboxIsSelected = selectedCount > 0 ? selectedCount == datagrid.Items.Count ? true : (bool?)null : false;
//if (selectedCount > 0)
//{
// if (selectedCount == datagrid.Items.Count)
// {
// CheckboxIsSelected = true;
// }
// else
// {
// CheckboxIsSelected = null;
// }
//}
//else
//{
// CheckboxIsSelected = false;
//}
}
/// <summary>
/// 全选和反选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbCheck_Click(object sender, RoutedEventArgs e)
{
var items = datagrid.ItemsSource as List<Person>;
if (items != null)
{
var result = (sender as CheckBox).IsChecked == true ? true : false;
items.ForEach(s => s.IsSelected = result);
}
}
model代码
public class Person : ViewModelBase
{
private bool _IsSelected;
/// <summary>
///
/// </summary>
public bool IsSelected
{
get { return _IsSelected; }
set
{
_IsSelected = value;
RaisePropertyChanged("IsSelected");
}
}
private string _Name; public string Name
{
get { return _Name; }
set
{
_Name = value;
RaisePropertyChanged("Name");
}
}
private string _Age; public string Age
{
get { return _Age; }
set
{
_Age = value;
RaisePropertyChanged("Age");
}
}
private string _Sex; public string Sex
{
get { return _Sex; }
set
{
_Sex = value;
RaisePropertyChanged("Sex");
}
}
}
ViewModelBase
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}