I am using MVVM and am displaying two listboxes on one window. I am binding from both of those listboxes simultaneously to different fields call them A and B. A and B are then both modifying C. To make this work, I want to only have one item from the two listboxes IsSelected at once, so that A does not override C when B IsSelected. How can I restrict this?
Share
I can think of a couple of ways to do this.
One way is you could bind the
ListBox.SelectedIndexof your 2 ListBoxes to change-notifying ViewModel properties.For example in your View:
And in your ViewModel:
Another way would be with an attached property like ‘GroupName’ where you can group Selectors (
ListBoxinherits fromSelector) to ensure only oneSelectorin the group has a selected item at any one time.For example:
And in your View:
If you have to click an item twice before it is highlighted you can use a quick workaround like this: