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

3
我有一个包含复选框的 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# 代码

   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个回答

4
ListBoxItem 模板中添加对 IsChecked 属性的绑定。
<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 类应该实现 INotifyPropertyChanged 接口才能使其正常工作。
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));
        }
    }
}

谢谢你的回答,Mitya。但我不知道如何将INotifyPropertyChanged实现到Authority类中。你能帮我吗? - Barsblue
谢谢你,Mitya。我添加了你写的代码。但是我遇到了错误,错误提示为“类型'Authority'已经包含了'AuthorityValue'和'AuthorityName'的'Ischecked'定义。我该怎么处理这些错误? - Barsblue
@Baris,请使用我编写的新版本替换你旧的Authority类。 - Mitya
@Baris,请再次查看编辑,一个括号位置不正确。 - Mitya

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

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

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}"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接