1

I have a viewmodel with a SelectedDocument property and a Documents property.

The Ui has a custom ListView control with ItemsSource bound to Documents and SelectedValue to SelectedDocument on the viewmodel.

Now in the viewmodel in the setter of the selected document in some situation I need to raise a dialog service message asking something, and in case of cancel I need to prevent changing the SelectedDocument.

I can do that but when I don't change the value of the property in the viewmodel I get the UI and the ViewModel out of sync.

It probably has soemthing to do due to the fact that the setter of the property is called when the two way binding is updating the source.

How can this be solved?

Thanks

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61

3 Answers3

3

You have to undo the change, but you cannot do so directly in the Setter but have to use the Dispatcher as discribed here.

As for using the Dispatcher in MVVM, there are various posts about using an IDispatcher interface for that, as can be found here or here or here (look under the MEFedMVVM.Services.Contracts namespace). You'll have to see which one of those works for you.

Jonathan Twite
  • 932
  • 10
  • 24
shriek
  • 5,157
  • 2
  • 36
  • 42
  • Right now I am using the dispatcher but this has sideeffects like fast navigation on a machine that is very low on resources means you can actually change the selected item twice before both dispatcher actions occur. An more than that to look at an "animation" afterwards. – Marino Šimić May 11 '12 at 08:32
  • Kudos to Nathan Allen-Wagner for the Dispatcher solution - and you, Shriek, for finding it! – else Aug 20 '15 at 10:10
1

I assume your using .Net 3.5? Its a 'known issue' and has been changed in .Net 4 (note I say changed not necessarily fixed!)

Theres a blog post here about the change, it also points to some suggestions about how to deal with the issue in 3.5

https://web.archive.org/web/20150925210516/https://karlshifflett.wordpress.com/2009/05/27/wpf-4-0-data-binding-change-great-feature/

Jonathan Twite
  • 932
  • 10
  • 24
Scott
  • 533
  • 1
  • 3
  • 10
  • I am using 4.0 but not for a Text property. For that one it works. I need something liek this for the SelectedValue/SelectedItem for the listbox. I need to prevent the listbox from changing the current item. – Marino Šimić May 11 '12 at 08:30
  • Ok, I have an attached property that I use on combo boxes to deal with the same issue. Essentially you set the property to true which subscribes it to the selection changed event. When the selection changed event is raised it retrieves the binding for SelectedItem and calls UpdateTarget. Can post the code if you like – Scott May 13 '12 at 23:17
0

You should call your PropertyChange event event if the dialog is cancelled. This way the UI layer will respond by resetting SelectedDocument.

Martin Doms
  • 8,598
  • 11
  • 43
  • 60
  • this is the first thing i tried, but whatever i do inside the property setter is not listened by the UI, probably because it is currently IN the binding prcedure which updates the viewmodel. maybe that could lead to a stack overflow so it does not care ?! – Marino Šimić May 10 '12 at 21:49