使用datagrid做全选全不选(拿出你得Codeing)
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好像不能自定义!
有没有简单点得实现方法!假的我也有!
假的就算了!希望大家能交流下!
...全文
相关推荐
WPF DATAGRID全选全不选
-- 其他列定义 --></DataGrid.Columns>```3. **添加全选/全不选按钮**:在UI上添加两个按钮,分别用于全选和全不选操作。
在.NET DataGrid中实现多选、单选和全选的CheckBox功能
DataGrid控件是许多现代应用程序中不可或缺的一部分,特别是当需要向用户提供复杂信息展示时。一个典型的DataGrid控件包括列(Columns)、行(Rows)和单元格(Cells),并且具备排序、筛选和分页等基础功能。理解DataGrid的结构是有效使用它进行数据展示和管理的关键。MVVM(Model-View-ViewModel)是一种软件架构模式,它将应用程序分为三个主要部分:Model、View和ViewModel。
flex中dataGrid全选和全不选功能
而在实际应用中,全选和全不选功能是数据列表中常见的需求,尤其是在需要批量操作场景下。下面我们将详细探讨如何在Flex的DataGrid中实现全选和全不选的功能。
WPF-DataGrid中CheckBox实现全选与非全选
可以使用`INotifyPropertyChanged`接口来通知UI更新。
DataGrid全选并获取选中条目数
**全选/全不选逻辑** - 在CheckBox_Checked事件处理函数中,遍历DataGrid的所有行,设置它们的IsSelected属性。


