WPF listView中【checkbox】实现全选功能

时间:2021-03-01 19:38:01
  List<xxx> nn = new List<xxx>();
public MainWindow()
{
InitializeComponent();

for (int i = 0; i < 10; i++)
{
nn.Add(new xxx { name = "nihaohao" + i, bol = false });
}
listview.ItemsSource = nn;
}

// public bool bb { get; set; }

private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (xxx item in nn)
{
//Debug.WriteLine(item.name + ":" + item.bol);
item.bol = true; //吧列表中checkbox属性设置为true
}
}

//xxx 表类
//实现INotifyPropertyChanged实现更改通知
public class xxx : INotifyPropertyChanged
{
private string _name;
public string name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("name");
}
}

private bool _bol;
public bool bol
{
get { return _bol; }
set
{
_bol = value;
OnPropertyChanged("bol");
}
}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string args)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(args));
Debug.WriteLine(name);
}
}