0

Short story

I have a BindingExpression object, I want to update the target side of the binding just like I can do it with the source (by calling BindingExpression.UpdateSource). How can I do it?

There is the similar question for WPF which I don't know how to adapt to Silverlight: Cancel combobox selection in WPF with MVVM

Long story

Given a two-way binding that binds the SelectedItem of a combobox to a property of a view model. The combobox is used for navigation so that by selecting the user tells the view model to navigate away. The view model can have some unsaved changes which the user don't want to loose accidently. So the view model throws out a confirmation dialog asking if the user really wants to navigate away and loose their changes. In case the user says no, we want to undo the selection made to the combobox, so that it was in it's original state just like before the navigation attempt was made. How can I do it?

For WPF people Please note, there is no UpdateTarget method in Silverlight (as far as I know), so this is why this question was brought up.

Community
  • 1
  • 1
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159

1 Answers1

1

If i understand you correctly, when the property changes, you should store the previous value of the SelectedItem backing field on an atrribute on your view model, and when the user decides to cancel the confirmation dialog, you should restore the SelectedItem backing field to the previous value.

If you're using regular properties for the backing field, you can write a Set method that implements this behavior:

    private object selectedItemPreviousValue;
    private object selectedItemBackingField;
    public object SelectedItemBackingField
    {
        get
        {
            return selectedItemBackingField;
        }
        set
        {
            selectedItemPreviousValue = selectedItemBackingField;
            selectedItemBackingField = value;
        }
    }

If you're using dependencie properties, you have to provide a PropertyMetada with a DependencyPropertyChantged callback, something like this:

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;
    }
andyroschy
  • 499
  • 3
  • 11
  • Yeah, I ended up doing this. However I wish somebody told me how to update the target side of a binding having the BindingExpression object at hands. – Trident D'Gao Dec 05 '12 at 18:21