我有
<DataGridCheckBoxColumn Binding="{Binding Path=Foo,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
和
public bool Foo{ get; set; }
检查/取消选中设置Foo,但在代码中设置Foo不会更改复选框状态.任何Suggesitons?
解决方法
当您在DataContext中设置Foo时,您需要调出PropertyChanged事件.通常,它看起来像:
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应该被更新