09/02/2023
Tham khảo mã nguồn phần XAML
Tham khảo mã nguồn phần Code
/// <summary>
/// Interaction logic for ListViewCheckBoxDemo.xaml
/// </summary>
public partial class ListViewCheckBoxDemo : Window
{
public ListViewCheckBoxDemo()
{
InitializeComponent();
Employees f_ep = new Employees();
ListCheckbox.DataContext = null;
ListCheckbox.ItemsSource = f_ep.List_EmployeesDefaull();
}
private void chkSelectAll_Click(object sender, RoutedEventArgs e)
{
if (chkSelectAll.IsChecked.Value == true)
{
ListCheckbox.SelectAll();
}
else
{
ListCheckbox.UnselectAll();
}
}
private void chkWspSelect_Checked(object sender, RoutedEventArgs e)
{
ListBoxItem item = ItemsControl.ContainerFromElement(ListCheckbox, e.OriginalSource as DependencyObject) as ListBoxItem;
if (item != null)
{
item.IsSelected = true;
}
}
private static bool individualChkBxUnCheckedFlag { get; set; }
private void chkWspSelect_Unchecked(object sender, RoutedEventArgs e)
{
ListBoxItem item = ItemsControl.ContainerFromElement(ListCheckbox, e.OriginalSource as DependencyObject) as ListBoxItem;
if (item != null)
item.IsSelected = false;
individualChkBxUnCheckedFlag = true;
CheckBox headerChk = (CheckBox)((GridView)ListCheckbox.View).Columns[0].Header;
headerChk.IsChecked = false;
}
}
public class Employees
{
public int ID { get; set; }
public string Name { get; set; }
public Employees() { }
public Employees(int id, string name)
{
ID = id;
Name = name;
}
public List<Employees> List_EmployeesDefaull()
{
List<Employees> List_Items = new List<Employees>();
List_Items.Add(new Employees(ID = 1, Name = "Đô rê mon"));
List_Items.Add(new Employees(ID = 2, Name = "Nô bi ta"));
List_Items.Add(new Employees(ID = 3, Name = "Xu ka"));
List_Items.Add(new Employees(ID = 4, Name = "Chai en"));
List_Items.Add(new Employees(ID = 5, Name = "Xê cô"));
List_Items.Add(new Employees(ID = 6, Name = "Đê khi"));
List_Items.Add(new Employees(ID = 7, Name = "Đô rê mi"));
return List_Items;
}
}