如果我理解正确,当属性更改时,您应该将SelectedItem
支持字段的先前值存储在视图模型的属性上,并且当用户决定取消确认对话框时,您应该将SelectedItem
支持字段恢复为以前的值.
如果您为支持字段使用常规属性,则可以编写一个实现此行为的 Set 方法:
private object selectedItemPreviousValue;
private object selectedItemBackingField;
public object SelectedItemBackingField
{
get
{
return selectedItemBackingField;
}
set
{
selectedItemPreviousValue = selectedItemBackingField;
selectedItemBackingField = value;
}
}
如果您使用依赖属性,则必须提供PropertyMetada
一个DependencyPropertyChantged
回调,如下所示:
public object SelectedItemBackingField
{
get { return (object)GetValue(SelectedItemBackingFieldProperty); }
set { SetValue(SelectedItemBackingFieldProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedItemBackingField. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedItemBackingFieldProperty =
DependencyProperty.Register("SelectedItemBackingField", typeof(object), typeof(MapApp), new PropertyMetadata(new PropertyChangedCallback(OnSelectedItemChanged));
public static OnSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
MyViewModel vm = (MyViewModel)sender;
vm.selectedItemPreviousValue = args.OldValue;
}