我正在使用 wpfToolKit 中的 checkedListbox 控件,我想在按下按钮时检查列表中的所有复选框,但它不起作用。
xml
<xctk:CheckListBox Command="{Binding CheckBoxClickedCommand}"
ItemsSource="{Binding ChosenFiles, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"/>
ViewModel
public ObservableCollection ChosenFiles { get; 放; }
模型
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;
}
窗口.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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句