如何在C#WPF中选中/取消选中列表框中的所有复选框? [英] How Can I Check/UnCheck All Checkboxes in Listbox in C# WPF?
问题描述
我有一个 ListBox
,其中包含 DataTemplate
中的复选框,我有两个按钮"Select All"和"UnSelect All".通过单击选择和取消选择按钮取消全部选中,我想实现 INotifyPropertyChanged
进行分类.我该怎么办?
I have a ListBox
contains checkboxes in DataTemplate
and I have two buttons "Select All" and "UnSelect All".I want to make that checkboxes to check all and uncheck all with clicking select and unselect buttons and I want to implement INotifyPropertyChanged
to class. How Can I do that things?
谢谢您的回答.
XAML代码
<StackPanel>
<ListBox Name="lstUserDefination"
ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<CheckBox Name="chkUser" Content="{Binding AuthorityName}"/>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
C#代码
public partial class UserDefinationEdit : Window
{
public ObservableCollection<Authority> authorityList { get; set; }
public UserDefinationEdit()
{
InitializeComponent();
CreateCheckBoxList();
lstUserDefination.ItemsSource = authorityList;
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
}
public void CreateCheckBoxList()
{
authorityList = new ObservableCollection<Authority>();
authorityList.Add(new Authority() {AuthorityValue = 0, AuthorityName = "0 - " });
authorityList.Add(new Authority() { AuthorityValue = 1, AuthorityName = "1 - " });
authorityList.Add(new Authority() { AuthorityValue = 2, AuthorityName = "2 - " });
authorityList.Add(new Authority() { AuthorityValue = 3, AuthorityName = "3 - " });
authorityList.Add(new Authority() { AuthorityValue = 4, AuthorityName = "4 - " });
authorityList.Add(new Authority() { AuthorityValue = 5, AuthorityName = "5 - " });
authorityList.Add(new Authority() { AuthorityValue = 6, AuthorityName = "6 - " });
this.DataContext = this;
}
private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
lstUserDefination.UnselectAll();
}
private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
lstUserDefination.SelectAll();
}
}
public class Authority
{
public string AuthorityName { get; set; }
public int AuthorityValue { get; set; }
public bool IsChecked { get; set; }
}
}
推荐答案
为 ListBoxItem
模板中的 IsChecked
属性添加绑定
Add binding for IsChecked
property in ListBoxItem
template
<CheckBox Name="chkUser" Content="{Binding AuthorityName}" IsChecked="{Binding IsChecked}"/>
并将按钮处理程序更改为
And change your button handlers to
private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
foreach (var a in authorityList)
{
a.IsChecked = false;
}
}
private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (var a in authorityList)
{
a.IsChecked = true;
}
}
请注意,您的 Authority
类应实现 INotifyPropertyChanged
才能使此项工作.
Note that your Authority
class should implement INotifyPropertyChanged
to make this work.
public class Authority : INotifyPropertyChanged
{
private string authorityName;
private int authorityValue;
private bool isChecked;
public string AuthorityName
{
get { return authorityName; }
set
{
authorityName = value;
NotifyPropertyChanged();
}
}
public int AuthorityValue
{
get { return authorityValue; }
set
{
authorityValue = value;
NotifyPropertyChanged();
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这篇关于如何在C#WPF中选中/取消选中列表框中的所有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!