如何使 WPF 滑块仅捕捉到离散的整数位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/173009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you make a WPF slider snap only to discrete integer positions?
提问by cplotts
All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of old. That is, I want a slider that goes from X to Y but only allows the user to move it in discrete integer positions.
我经常想要一个行为类似于旧的 System.Windows.Forms.TrackBar 的 WPF 滑块。也就是说,我想要一个从 X 到 Y 的滑块,但只允许用户在离散的整数位置移动它。
How does one do this in WPF since the Value property on the Slider is double?
由于 Slider 上的 Value 属性是双倍,因此在 WPF 中如何做到这一点?
采纳答案by Brian Stewart
回答by cplotts
The simple answer is that you take advantage of the IsSnapToTickEnabledand TickFrequencyproperties. That is, turn snapping to ticks on and set the tick frequency to 1.
简单的答案是您利用IsSnapToTickEnabled和TickFrequency属性。也就是说,打开捕捉到刻度并将刻度频率设置为 1。
Or, in other words ... take advantage of ticks ... but you don't necessarily have to show the ticks that you are snapping to.
或者,换句话说......利用刻度线......但您不一定必须显示您要捕捉到的刻度线。
Check out the following piece of xaml:
查看以下 xaml 片段:
<Slider
Orientation="Vertical"
Height="200"
Minimum="0"
Maximum="10"
Value="0"
IsSnapToTickEnabled="True"
TickFrequency="1"
/>
回答by Dave
For those that want to snap to specificpositions, you can also use the Ticks
property:
对于那些想要捕捉到特定位置的人,您还可以使用该Ticks
属性:
<Slider Minimum="1" Maximum="500" IsSnapToTickEnabled="True" Ticks="1,100,200,350,500" />
回答by mkjeldsen
The snap trick is handy but has limitations, for instance if you want to only show a subset of valid ticks. I've had success with two alternatives: either bind to an integer or round the new value. Here is a combined example:
捕捉技巧很方便,但有局限性,例如,如果您只想显示有效刻度的子集。我有两种选择取得了成功:绑定到一个整数或舍入新值。这是一个组合示例:
public int MyProperty { get; set; }
private void slider1_ValueChanged(object sender,
RoutedPropertyChangedEventArgs<double> e)
{
(sender as Slider).Value = Math.Round(e.NewValue, 0);
}
<Slider
Name="slider1"
TickPlacement="TopLeft"
AutoToolTipPlacement="BottomRight"
ValueChanged="slider1_ValueChanged"
Value="{Binding MyProperty}"
Minimum="0" Maximum="100" SmallChange="1" LargeChange="10"
Ticks="0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100"/>
I have no idea how the performance of either compares to the snap trick but I haven't had any trouble*.
我不知道两者的性能与快速技巧相比如何,但我没有遇到任何问题*。
*If you alsobind the value of the slider to a type of text field you will experience that, every once in a while if using the mouse, the text field will show decimals. If you also bind to an int at the same time the empty string will cause a conversion exception to be thrown that briefly bogs down the UI. These issues haven't been severe enough for me to look for solutions.
*如果您还将滑块的值绑定到一种类型的文本字段,您会遇到这种情况,如果使用鼠标,文本字段每隔一段时间就会显示小数。如果您同时绑定到一个 int 空字符串将导致抛出转换异常,这会短暂地使 UI 陷入困境。这些问题还不够严重,我无法寻找解决方案。