如何检查checkedListBox wpfToolkit中的所有复选框

时间:2018-11-16 16:51:14

标签: wpf mvvm viewmodel wpftoolkit

我正在使用wpfToolKit的checkedListbox控件,我想在按下按钮但不起作用时检查列表中的所有复选框。

Xaml

 <xctk:CheckListBox  Command="{Binding CheckBoxClickedCommand}" 
    ItemsSource="{Binding ChosenFiles,  UpdateSourceTrigger=PropertyChanged}" 
    DisplayMemberPath="Name"/>

ViewModel
     公共ObservableCollection ChosenFiles {get;组; }

模型

public class ChosenFile{
    public string FullPath { get; set; }
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}

当我更改IsChecked属性时,我希望我的checkedListbox更新吗?

1 个答案:

答案 0 :(得分:1)

这是您可以怎么做

首先按如下所示重新定义“ ChosenFile”类,以连接INotifyPropertyChanged接口

public class ChosenFile : INotifyPropertyChanged
{
    private string _fullPath;
    public string FullPath
    {
        get { return _fullPath; }
        set
        {
            _fullPath = value;
            OnPropertyChanged();
        }
    }
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

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

    private void OnPropertyChanged([CallerMemberName] string propName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

Window.xaml

    <Button Command="{Binding CheckBoxClickedCommand}" Width="100"> Check All</Button>
    <xctk:CheckListBox ItemsSource="{Binding ChosenFiles}" DisplayMemberPath="Name" SelectedMemberPath="IsChecked" />

在后面的代码中,在“ CheckBoxClickedCommand”执行方法上,执行此操作

        foreach (var rec in ChosenFiles)
            rec.IsChecked = true;