我正在使用来自WPF的CheckListBox,并试图将它绑定到我的ViewModel上。除了从控件中获取选定的值之外,我还希望能够通过单击按钮来重置它,这将清除任何选择。我被困在如何绑定集合中每一项的选定或检查状态,但如果我的整个方法是关闭的,我也希望在这方面有一些方向。
我创建了一个带有字符串描述符和布尔属性的简单类,我计划使用这些属性来指示每个复选框的状态.
public class DrugInfluence : INotifyPropertyChanged
{
public string Impairment { get; set; }
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
if(isChecked != value)
{
isChecked = value;
OnPropertyChanged("IsChecked");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}一个名为DrugInfluence的ViewModel中的ImpairmentList对象集合,我想将它绑定到CheckListBox控件本身.
public List<DrugInfluence> ImpairmentList
{
get
{
return impairmentList;
}
set
{
if(impairmentList != value)
{
impairmentList = value;
NotifyPropertyChanged("ImpairmentList");
}
}
}我用来将ViewModel绑定到CheckListBox控件的XAML .
<sdk:CheckListBox Margin="6"
ItemsSource="{Binding ImpairmentList}"
DisplayMemberPath="Impairment"
SelectedMemberPath="IsChecked"
SelectedItemsOverride="{Binding SelectedImpairments, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</sdk:CheckListBox>发布于 2016-11-18 18:30:49
这是您正在使用的控件吗?如果是这样的话,看起来SelectedMemberPath就是要绑定到的属性。如果要以编程方式检查/取消检查,还需要在IsChecked属性更改时引发事件。例如,
public class DrugInfluence : INotifyPropertyChanged
{
public string Impairment { get; set; }
private bool _isChecked;
public bool IsChecked
{
get{ return _isChecked;}
set
{
if (_isChecked!= value)
{
_isChecked= value;
OnPropertyChanged("IsChecked");
}
};
}
}然后实现其余的INotifyPropertyChanged成员
https://stackoverflow.com/questions/40683649
复制相似问题