如何在C#WPF中检查/取消选中列表框中的所有复选框?

时间:2017-08-18 08:07:15

标签: c# wpf checkbox listbox observablecollection

我在ListBox中有一个DataTemplate包含复选框,我有两个按钮“全选”和“取消全选”。我想要选中所有选项并通过单击选择取消选中全部取消选择按钮,我想实现INotifyPropertyChanged到课堂。我该怎么办呢?

提前感谢您的回答..

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#CODE

   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; }
}
}

2 个答案:

答案 0 :(得分:3)

(&x).m()模板

中为&属性添加绑定
IsChecked

将按钮处理程序更改为

ListBoxItem

请注意,您的<CheckBox Name="chkUser" Content="{Binding AuthorityName}" IsChecked="{Binding IsChecked}"/> 课程应实施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

答案 1 :(得分:1)

实施INotifyPropertyChanged

public class Authority : INotifyPropertyChanged
{
    private string _authorityName;
    public string AuthorityName
    {
        get { return _authorityName; }
        set { _authorityName = value; NotifyPropertyChanged(); }
    }

    private string _authorityValue;
    public string AuthorityValue
    {
        get { return _authorityValue; }
        set { _authorityValue = value; NotifyPropertyChanged(); }
    }

    private bool  _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

设置所有IsChecked个对象的Authority属性:

private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
    Select(false);
}

private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
    Select(true);
}

private void Select(bool select)
{
    foreach (Authority authority in authorityList)
        authority.IsChecked = select;
}

绑定XAML中的IsChecked属性:

<CheckBox Name="chkUser" IsChecked="{Binding IsChecked}" Content="{Binding AuthorityName}"/>