我有
<DataGridCheckBoxColumn
Binding="{Binding Path=Foo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
和
public bool Foo{ get; set; }
选中/取消选中设置
Foo
,但是在代码中设置Foo
不会更改Checkbox状态。有建议吗? 最佳答案
在PropertyChanged
中设置Foo时,需要引发DataContext
事件。通常,它看起来像:
public class ViewModel : INotifyPropertyChanged
{
private bool _foo;
public bool Foo
{
get { return _foo; }
set
{
_foo = value;
OnPropertyChanged("Foo");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
如果调用
Foo = someNewvalue
,则会引发PropertyChanged
事件,并且应该更新您的UI关于wpf - WPF CheckBox TwoWay绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16094679/