Suppose I have two comboboxes. One for the lower-value and one for the upper value. The user must select both values. To make it comfortable for her, I’d like to prevent the error (lower>upper) by limiting the values in the upper-value-combobox.
<example>
Here an example. (In the example, I use integers – my real world problem has different objects.)
-
The user has the choice within the range [1..5]:
lower: [1, 2, 3, 4, 5] --> no lower-value selected upper: [1, 2, 3, 4, 5] --> no upper-value selected -
If she selects 3 as the lower value, I’d like the upper-checkbox to provide only the values [3..5]. This works fine by changing the databound ObservableCollection<MyObj> of the upper-combobox.
lower: [1, 2, __3__, 4, 5] --> User has selected '3' upper: [3, 4, 5] --> after that, only values within the range [3..5] are available. -
User selects 4 as the upper-value
lower: [1, 2, __3__, 4, 5] --> User has selected '3' upper: [3, __4__, 5] --> User has selected '4' -
User changes his mind and selects 2 as the lower value
lower: [1, __2__, 3, 4, 5] --> User has selected '2' upper: [2, 3, __4__, 5] --> '4' should be kept selected
</example>
In the windows-forms world, I would have created a user-control and control the event-handling myself. In fact, I would turn off the handling of SelectedIndexChanged event of the upper-combobox, adjust it’s underlying list, set the appropriate index and turn the eventhandling back on again.
I’ve encountered a weird problem in the forth step. I found no way to keep the selected value while changing the underlying collection.
- What’s the mvvm-way of dealing with concerns like these? Or isn’t the mvvm-pattern the right medicine for my scenario?
- Is this the right place for a usercontrol, where I have full control over the event-handling?
- Is Expression-Blend really built using the mvvm-pattern? 😉
instead of create a new upper collection everytime, why not simply use a ICollectionView.Filter? so you can hold your selected item.
EDIT: fast and dirty example 😉
usercontrol
xaml