using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;namespace Infragistics.Samples.WPF.xamFeatureBrowser.Samples.XamDataGrid
{
public partial class CheckBoxInRecordSelectors_Samp : Page
{
public CheckBoxInRecordSelectors_Samp()
{
InitializeComponent(); List<PersonViewModel> members =
(from p in Person.GetPeople()
select new PersonViewModel(p))
.ToList(); base.DataContext = new CommunityViewModel(members);
} void OnShowRecords(object sender, RoutedEventArgs e)
{
List<string> checkedNames = new List<string>();
List<string> uncheckedNames = new List<string>(); var community = base.DataContext as CommunityViewModel;
foreach (PersonViewModel p in community.Members)
{
if (p.IsChecked)
checkedNames.Add(p.LastName);
else
uncheckedNames.Add(p.LastName);
} checkedNames.Sort();
uncheckedNames.Sort(); StringBuilder sb = new StringBuilder(); sb.AppendLine(Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Checked);
foreach (string s in checkedNames)
sb.AppendLine(s); sb.AppendLine(); sb.AppendLine(Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Unchecked);
foreach (string s in uncheckedNames)
sb.AppendLine(s); string msg = sb.ToString();
string caption = Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Caption;
MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Information);
} #region Person [nested class] /// <summary>
/// A simple data object that stores information about a person.
/// </summary>
public class Person
{
public static Person[] GetPeople()
{
return new Person[]
{
new Person("Jane", "Smith", 23),
new Person("Mike", "Wheeler", 45),
new Person("Ned", "Ableton", 67),
new Person("Patrick", "McCort", 12),
new Person("Ravi", "Patel", 28),
new Person("Wallace", "Barrington", 42),
};
} public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
} public int Age { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
} #endregion // Person [nested class] #region PersonViewModel [nested class] /// <summary>
/// A presentation-friendly wrapper for the Person class, which has support for being 'checked.'
/// </summary>
public class PersonViewModel : INotifyPropertyChanged
{
readonly Person _person;
bool _isChecked; public PersonViewModel(Person person)
{
_person = person;
} public bool IsChecked
{
get { return _isChecked; }
set
{
if (value == _isChecked)
return; _isChecked = value; this.OnPropertyChanged("IsChecked");
}
} public int Age { get { return _person.Age; } }
public string FirstName { get { return _person.FirstName; } }
public string LastName { get { return _person.LastName; } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
} #endregion // INotifyPropertyChanged Members
} #endregion // PersonViewModel [nested class] #region CommunityViewModel [nested class] /// <summary>
/// A presentation-friendly class that contains a list of PersonViewModel objects
/// and manages an aggregated check state for the group, via the get/set property
/// called AllMembersAreChecked.
/// </summary>
public class CommunityViewModel : INotifyPropertyChanged
{
public CommunityViewModel(List<PersonViewModel> members)
{
this.Members = members; foreach (PersonViewModel member in members)
member.PropertyChanged += (sender, e) =>
{
// Raising PropertyChanged for the AllMembersAreChecked
// property causes the data binding system to query its
// getter for the new value.
if (e.PropertyName == "IsChecked")
this.OnPropertyChanged("AllMembersAreChecked");
};
} public List<PersonViewModel> Members { get; private set; } public bool? AllMembersAreChecked
{
get
{
// Determine if all members have the same
// value for the IsChecked property.
bool? value = null;
for (int i = 0; i < this.Members.Count; ++i)
{
if (i == 0)
{
value = this.Members[0].IsChecked;
}
else if (value != this.Members[i].IsChecked)
{
value = null;
break;
}
} return value;
}
set
{
if (value == null)
return; foreach (PersonViewModel member in this.Members)
member.IsChecked = value.Value;
}
} #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
} #endregion // INotifyPropertyChanged Members
} #endregion // CommunityViewModel [nested class]
} public class CheckBoxInRecordSelectors_Samp_Root
: Infragistics.Samples.WPF.xamFeatureBrowser.Controls.LocalizationRoot
{
public string Button_ShowRecords
{
get { return Strings.ControlComposition_CheckBoxInRecordSelectors_Button_ShowRecords; }
}
}
}这个是我看的一个例子 可以实现全选全不选 但是Header好像不能自定义!
有没有简单点得实现方法!假的我也有!
假的就算了!希望大家能交流下!
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;namespace Infragistics.Samples.WPF.xamFeatureBrowser.Samples.XamDataGrid
{
public partial class CheckBoxInRecordSelectors_Samp : Page
{
public CheckBoxInRecordSelectors_Samp()
{
InitializeComponent(); List<PersonViewModel> members =
(from p in Person.GetPeople()
select new PersonViewModel(p))
.ToList(); base.DataContext = new CommunityViewModel(members);
} void OnShowRecords(object sender, RoutedEventArgs e)
{
List<string> checkedNames = new List<string>();
List<string> uncheckedNames = new List<string>(); var community = base.DataContext as CommunityViewModel;
foreach (PersonViewModel p in community.Members)
{
if (p.IsChecked)
checkedNames.Add(p.LastName);
else
uncheckedNames.Add(p.LastName);
} checkedNames.Sort();
uncheckedNames.Sort(); StringBuilder sb = new StringBuilder(); sb.AppendLine(Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Checked);
foreach (string s in checkedNames)
sb.AppendLine(s); sb.AppendLine(); sb.AppendLine(Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Unchecked);
foreach (string s in uncheckedNames)
sb.AppendLine(s); string msg = sb.ToString();
string caption = Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Caption;
MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Information);
} #region Person [nested class] /// <summary>
/// A simple data object that stores information about a person.
/// </summary>
public class Person
{
public static Person[] GetPeople()
{
return new Person[]
{
new Person("Jane", "Smith", 23),
new Person("Mike", "Wheeler", 45),
new Person("Ned", "Ableton", 67),
new Person("Patrick", "McCort", 12),
new Person("Ravi", "Patel", 28),
new Person("Wallace", "Barrington", 42),
};
} public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
} public int Age { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
} #endregion // Person [nested class] #region PersonViewModel [nested class] /// <summary>
/// A presentation-friendly wrapper for the Person class, which has support for being 'checked.'
/// </summary>
public class PersonViewModel : INotifyPropertyChanged
{
readonly Person _person;
bool _isChecked; public PersonViewModel(Person person)
{
_person = person;
} public bool IsChecked
{
get { return _isChecked; }
set
{
if (value == _isChecked)
return; _isChecked = value; this.OnPropertyChanged("IsChecked");
}
} public int Age { get { return _person.Age; } }
public string FirstName { get { return _person.FirstName; } }
public string LastName { get { return _person.LastName; } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
} #endregion // INotifyPropertyChanged Members
} #endregion // PersonViewModel [nested class] #region CommunityViewModel [nested class] /// <summary>
/// A presentation-friendly class that contains a list of PersonViewModel objects
/// and manages an aggregated check state for the group, via the get/set property
/// called AllMembersAreChecked.
/// </summary>
public class CommunityViewModel : INotifyPropertyChanged
{
public CommunityViewModel(List<PersonViewModel> members)
{
this.Members = members; foreach (PersonViewModel member in members)
member.PropertyChanged += (sender, e) =>
{
// Raising PropertyChanged for the AllMembersAreChecked
// property causes the data binding system to query its
// getter for the new value.
if (e.PropertyName == "IsChecked")
this.OnPropertyChanged("AllMembersAreChecked");
};
} public List<PersonViewModel> Members { get; private set; } public bool? AllMembersAreChecked
{
get
{
// Determine if all members have the same
// value for the IsChecked property.
bool? value = null;
for (int i = 0; i < this.Members.Count; ++i)
{
if (i == 0)
{
value = this.Members[0].IsChecked;
}
else if (value != this.Members[i].IsChecked)
{
value = null;
break;
}
} return value;
}
set
{
if (value == null)
return; foreach (PersonViewModel member in this.Members)
member.IsChecked = value.Value;
}
} #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
} #endregion // INotifyPropertyChanged Members
} #endregion // CommunityViewModel [nested class]
} public class CheckBoxInRecordSelectors_Samp_Root
: Infragistics.Samples.WPF.xamFeatureBrowser.Controls.LocalizationRoot
{
public string Button_ShowRecords
{
get { return Strings.ControlComposition_CheckBoxInRecordSelectors_Button_ShowRecords; }
}
}
}这个是我看的一个例子 可以实现全选全不选 但是Header好像不能自定义!
有没有简单点得实现方法!假的我也有!
假的就算了!希望大家能交流下!
解决方案 »
- 请教C#.net循环读取数组问题
- 用new关键字定义值类型有什么意义??
- winform的线程中断问题
- oleDbDataAdapter的selectCommand用了别名后UpData出现的怪问题,100分送上,在线等......
- 推(Push)/拉(Pull)模式 难道真就不能在代码中体现出来吗?--看MVP文章小议
- 请大家看看这条SQL语句该怎么写,计算两日期间隔的天数, 多谢 :)
- 简明扼要。。。。。问个简单的小白问题,datagridview设计的时候,怎么样达到这个效果,,代码或者直接属性设置都行,,求指导
- 图片分块存储,显示
- 我们公司现在用的数据访问类,觉得有挺多欠缺,请高手给指点指点。
- 未处理的“System.Resources.MissingManifestResourceException”类型的异常出现在 mscorlib.dll 中
- 如何在 datagridview的单元格中嵌入 “组合框”、“文本框”进行编辑呢?
- VS2010创建的WCF Web Service第一次调用很慢的问题!!!
{
// 选中全选
if(checkedAll.Checked.Equals(true))
{
foreach(DataGridItem dgi in dtgShowData.Items)
{
CheckBox cb1 = (CheckBox)dgi.FindControl("checkBox1");
// 选中
cb1.Checked = true;
}
}
// 取消全选
if(checkedAll.Checked.Equals(false))
{
foreach(DataGridItem dgi in dtgShowData.Items)
{
CheckBox cb2 = (CheckBox)dgi.FindControl("checkBox1");
// 取消选中
cb2.Checked = false;
}
}
}<!--页面:Datagrid外位置增加一个复选框asp:CheckBox。选中则为全选,取消选中则为全不选。-->
<asp:CheckBox Runat="server" ID="checkedAll" AutoPostBack="True" Text="全选/全不选"></asp:CheckBox>
/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCheckAll_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < this.dgvStock.Rows.Count; i++)
{
DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgvStock.Rows[i].Cells["ISCheck"];
checkcell.Value = true;
}
}
catch (Exception exp)
{
MessageHelper.ShowInfo(this, exp.Message);
}
}
/// <summary>
/// 不全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCheckCancel_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < this.dgvStock.Rows.Count; i++)
{
DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgvStock.Rows[i].Cells["ISCheck"];
checkcell.Value = false;
}
}
catch (Exception exp)
{
MessageHelper.ShowInfo(this, exp.Message);
}
}
/// <summary>
/// 单选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvStock_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == -1 || e.RowIndex == -1) return;
DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgvStock.Rows[e.RowIndex].Cells["ISCheck"];
Boolean flag = Convert.ToBoolean(checkcell.Value);
if (flag)
{
checkcell.Value = false;
}
else
{
checkcell.Value = true;
}
}
catch (Exception exp)
{
MessageHelper.ShowInfo(this, exp.Message);
}
}
我有winfrom实现全选反选的demo
QQ453367672