两个相互依赖的滑块

时间:2021-09-13 18:04:21

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.

我定义了两个滑块,每个滑块的值介于0到200之间。一个滑块用于最小值,第二个滑块用于最大值。

Now a requirement is that minimum < maximum. I can ensure this requirment in the listeners method of the two sliders, 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?

现在要求是最小 <最大值。我可以在两个滑块的侦听器方法中确保这个要求,但我想防止用户甚至可以在滑块上选择不符合此要求的值。我怎样才能做到这一点?< p>

2 个解决方案

#1


3  

If you are using a presentation pattern, like MVVM…

如果你使用的是演示模式,比如MVVM ......

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

将每个滑块绑定到演示模型/视图模型中的属性。确保在绑定上设置Mode = TwoWay。

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.

现在,在视图模型中,您可以让您的setter完成工作。他们可以停在极限(设置其他财产的地方),也可以推动补充财产。例如,如果max设置为100,并且用户将min滑动到150,则还可以将max设置为150。只要您提出属性更改通知,用户也会看到另一个滑块移动。

#2


2  

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.

一种方法是将当前“最小”滑块值绑定到“最大”滑块的“最小”属性,反之亦然。当滑块重新定位时,这可能会导致略微奇怪的效果,但它肯定会阻止无效选择。

#1


3  

If you are using a presentation pattern, like MVVM…

如果你使用的是演示模式,比如MVVM ......

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

将每个滑块绑定到演示模型/视图模型中的属性。确保在绑定上设置Mode = TwoWay。

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.

现在,在视图模型中,您可以让您的setter完成工作。他们可以停在极限(设置其他财产的地方),也可以推动补充财产。例如,如果max设置为100,并且用户将min滑动到150,则还可以将max设置为150。只要您提出属性更改通知,用户也会看到另一个滑块移动。

#2


2  

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.

一种方法是将当前“最小”滑块值绑定到“最大”滑块的“最小”属性,反之亦然。当滑块重新定位时,这可能会导致略微奇怪的效果,但它肯定会阻止无效选择。