1

I am using WPF and MVVM Light framework.

I would like to know how to revert back the selection on a combobox.

For example:

  • Default selection is "Text 1"
  • When the user select "Text 2" in the combobox, I display a dialog box (Yes/No) to confirm the action
  • If the user clicks "No", I want to revert back the value to "Text 1"

So far my xaml is like this:

ComboBox ItemsSource="{Binding SourceData}" SelectedItem="{Binding SelectedSourceData,Mode=TwoWay}"

My binding SourceData is defined in my MainViewModel.cs as:

public ObservableCollection<TextItem> SourceData { get; set; }

I do not have a name for my combobox. Is there any way to revert the selection using a binding method?

Any help would be much appreciated.

Thanks.

Romain
  • 103
  • 2
  • 11

4 Answers4

1

(not tested)

In the ViewModel's Set of your bound property, call the Confirm Dialog before you set the value internally. Only set the private value if the answer is yes, then raise the property changed in either case.

This remains testable (if you have an mockable interface for the Confirm Dialog).

Patrice Calvé
  • 674
  • 6
  • 12
0

I just went through this with a list view

See this link

You have to use eventtocommand to bind to a relay command on the selecteditemchanged event of the combo box. The selection will change, but you can then validate your logic and change it back if you need to

Community
  • 1
  • 1
J King
  • 4,108
  • 10
  • 53
  • 103
0

ICollectionView is the most common used in term of selections.
MoveCurrentToPrevious() moves the selection from the current to the previous one, and that's what you're looking for. so, bind it to your ComboBox and just work with it ! for more info in MSDN hope it helps.

HichemSeeSharp
  • 3,240
  • 2
  • 22
  • 44
0

I had the same issue, causes by UI thread and the way that biding works. Check the this link: SelectedItem on ComboBox

The structure in the sample uses code behind but the MVVM is exactly the same.

Community
  • 1
  • 1
Fred Jand
  • 699
  • 7
  • 25