我使用的是wpfToolKit中的checkedListbox控件,我想在按下按钮但不起作用时选中列表中的所有复选框。
Xaml
<xctk:CheckListBox Command="{Binding CheckBoxClickedCommand}"
ItemsSource="{Binding ChosenFiles, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"/>
ViewModel
public ObservableCollection Chosen Files{get;set;}
型号
public class ChosenFile{
public string FullPath { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
}
我想在更改IsChecked属性时更新我的checkedListbox。可以用这个控件完成吗?
以下是
首先将"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;