我正在使用 ObservableCollection<MyItemViewModel> myItemVMList
作为 ItemsSouce
。我能够完美绑定Command
,但是 INotifyPropertyChanged
却无法工作。这是我的代码:
public class MyItemViewModel: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
public MyItem MyItem { set; get; }
private RelayCommand _ChangeMyItemPropertyValue;
public ICommand ChangeMyItemPropertyValueCommand {
get {
if (_ChangeMyItemPropertyValue == null) _ChangeMyItemPropertyValue = new RelayCommand(o => ChangeMyItemPropertyValue());
return _ChangeMyItemPropertyValue;
}
}
private ChangeMyItemPropertyValue() {
MyItem.SomeProperty = someDifferentValue;
// NEITHER OF THESE CALLS WORK
OnPropertyChanged("MyItem.SomeProperty");
OnPropertyChagned("SomeProperty");
}
}
毋庸置疑,DataTemplate
中的绑定已设置为 Content="{Binding MyItem.SomeProperty}"
,它会显示正确的值。问题在于当我运行函数时,它没有更新。
附注:如果我在 MyItem
中实现了 INotifyPropertyChanged
,它可以工作,但是我希望在 ViewModel
中实现。
{Binding MyItem.SomeProperty}
。 - Christopher FranciscoFoo.Bar.Baz
时,它将会监听Foo
和Foo.Bar
上的更改通知。在你的情况下,它会监听MyItem
上的更改通知。 - Lucas Trzesniewski