Yinon Dotan
Yinon Dotan

Reputation: 147

How to check all check boxes in checkedListBox wpfToolkit

I am using checkedListbox control form the wpfToolKit, I want to check all the checkboxes in the list when I press a button but its not working.

Xaml

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

ViewModel
public ObservableCollection ChosenFiles { get; set; }

Model

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

I want my checkedListbox to update when I change the IsChecked property can it be done with this control?

Upvotes: 0

Views: 310

Answers (1)

Cinchoo
Cinchoo

Reputation: 6322

Here is how you can do it

First redefine the 'ChosenFile' class as below to wire with INotifyPropertyChanged interface

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

On the code behind, on the 'CheckBoxClickedCommand' execute method, do this

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

Upvotes: 1

Related Questions