Romain
Romain

Reputation: 103

How to revert selection on combobox with MVVM Light

I am using WPF and MVVM Light framework.

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

For example:

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.

Upvotes: 1

Views: 420

Answers (4)

Fred Jand
Fred Jand

Reputation: 699

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.

Upvotes: 0

HichemSeeSharp
HichemSeeSharp

Reputation: 3318

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.

Upvotes: 0

J King
J King

Reputation: 4434

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

Upvotes: 0

Patrice Calv&#233;
Patrice Calv&#233;

Reputation: 694

(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).

Upvotes: 1

Related Questions