datagrid 绑定选中数据,列头全选
成品图:


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));
}
}
}
datagrid 绑定选中数据,列头全选的更多相关文章
- Easyui datagrid加载数据时默认全选的问题
问题描述: 最近使用 Easyui datagrid 展示数据,之前一直使用很正常,今天出现了一个怪异问题 加载数据后,只要点击选中列 ck 的任意行或多行,再刷新时整个datagrid的所有数据都 ...
- WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选)
原文:WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选) 前台代码 <DataGrid.Columns> <DataGridCheckB ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- springMvc接收ajax数组参数,以及jquery复选框选中、反选、全选、全不选
一.复选框选中.反选.全选.全不选 html代码: <input type='checkbox' name='menuCheckBox' value='10' >苹果 <input ...
- easyui datagrid checkbox复选框取消单击选中事件、初始全选全不选等问题解决
系统业务需要,导入的列表数据默认全部选中,且不可取消选中行.全部店铺优惠券发放过后导入的数据全部清空.如图所示: 一.初始化页面默认全部选中“selectAll”,全部不选中“unselectAll” ...
- JQuery EasyUI之DataGrid列名和数据列分别设置不同对齐方式(转)
需求如下 现有数据列三列 Name,Age,CreateDate 数据 张三,18,2000-12-09 :12:34:56 李四,28,2000-12-09 :12:34:56 王麻子,38,200 ...
- WPF DataGrid 绑定DataSet数据 自动生成行号
1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...
- asp:DataGrid之添加asp:CheckBox做全选功能时涉及到绑值问题解决
最大的意图是为asp:CheckBox的value绑定上自己需要的value值,而不是默认的字符串"on" 参考了这篇文章带Value属性的扩展CheckBox控件,意义不大,换了 ...
- jquery checkbox选中状态以及实现全选反选
jquery1.6以下版本获取checkbox的选中状态: $('.ck').attr('checked'); $('.ck').attr('checked',true);//全选 $('.ck'). ...
随机推荐
- 51nod 1513 && CF570D
题意:给定一棵树,每个节点有一个字母.给定若干个询问,询问某个子树内某一深度的节点是否能将这些节点组合成一个回文串.(深度是以根节点为基准的,不是当前子树根.)数据规模10^5. 神犇题解 子树问题, ...
- HBase常用操作-HBaseUtil
package com.zhen.hbase; import java.io.IOException; import java.util.ArrayList; import java.util.Col ...
- intel dpdk api interrupt module 中断模块介绍
声明:此文档只做学习交流使用,请勿用作其他商业用途 author:朝阳_tonyE-mail : linzhaolover@gmail.comCreate Date: 2013-7-12 11:46: ...
- Compilation error 未完待续
1. code.cpp:1:21: fatal error: iostream : No such file or directory #include< iostream > ^ com ...
- fatal error C1071: unexpected end of file found in comment
1.错误 #include<iostream> using namespace std; int main() { ..... return 0; } //如果把注释放到这里了,那么提交就 ...
- python suds 调用webservice 缓存
在linux系统中 如果webservice更新了字段 suds调用有可能缓存以前的字段或方法,对新的字段报找不到类型 TypeNotFound,或者对 新加的方法找不到该方法的错误. 当更新或添加w ...
- ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)
Problem Description In Geometry, the problem of track is very interesting. Because in some cases, th ...
- git rebase小计(转)
git rebase,顾名思义,就是重新定义(re)起点(base)的作用,即重新定义分支的版本库状态.要搞清楚这个东西,要先看看版本库状态切换的两种情况: 我们知道,在某个分支上,我们可以通过git ...
- Python:os.walk()和os.path.walk()用法
转于:https://www.cnblogs.com/zmlctt/p/4222621.html 博主:zmlctt 一.os.walk() 函数声明:os.walk(top,topdown=True ...
- ES6学习之字符串的扩展
字符的Unicode表示法 \uxxxx可以表示一个双字节的字符(xxxx表示字符编码,范围在0000---FFFF),超出此范围用两个双字节表示. console.log("\u0061& ...