开发者

Two sliders that depend on each other

开发者 https://www.devze.com 2023-02-02 09:48 出处:网络
I defined two sliders which can have values between 0 and 200 each. One slider is for the minimum value and the second is for the maximum value.

I defined two sliders which can have values between 0 and 200 each. One slider is for the minimum value and the second is for the maximum value.

Now a requirement is that minimum < maximum. I can ensure this requirment in the listeners method of the two sliders开发者_StackOverflow社区, but I would like to prevent that the user can even select values that don't meet this requirements on the slider. How can I do that?


If you are using a presentation pattern, like MVVM…

Bind each slider to a property in the presentation-model/view-model. Make sure you set Mode=TwoWay on the binding.

Now, in the view-model, you can make your setters do the work. They can either stop at the limit (where the other property is set), or also push the complementary property. For example, if max is set to 100, and the user slides min to 150, you can set max at 150 as well. As long as you're raising the property change notifications, the user will see the other slider move, too.


One way would be to bind the current "Minimum" slider value to the Minimum property of the Maximum slider and vice versa. This may lead to a slightly odd-feeling effect as the slider repositions itself, but it would certainly prevent invalid selections.

0

精彩评论

暂无评论...
验证码 换一张
取 消