是否有 WPF 控件来选择看起来像滑块的范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15814623/
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
Is there a WPF control to select a range looking like a slider?
提问by Michel Keijzers
Is there a WPF control looking like a slider (or maybe Slider itself), that has both a minimum and maximum value instead of a single value to let the user select a range?
是否有一个看起来像滑块(或者滑块本身)的 WPF 控件,它具有最小值和最大值而不是单个值来让用户选择一个范围?
采纳答案by kmatyaszek
You can use RangeSliderfrom AvalonControlsLibrary.
您可以使用RangeSlider从AvalonControlsLibrary。
Example:
例子:
<avalon:RangeSlider RangeStart="0" RangeStop="100"
RangeSelectionChanged="RangeSlider_RangeSelectionChanged"/>
Where avalon is:
阿瓦隆在哪里:
xmlns:avalon="http://schemas.AvalonControls/AvalonControlsLibrary/Controls"
RangeSelectionChanged event hadler:
RangeSelectionChanged 事件处理程序:
private void RangeSlider_RangeSelectionChanged(object sender, AC.AvalonControlsLibrary.Controls.RangeSelectionChangedEventArgs e)
{
Console.WriteLine("e.NewRangeStart: " + e.NewRangeStart);
Console.WriteLine("e.NewRangeStop: " + e.NewRangeStop);
}
回答by Patrick Quirk
The MahApps.Metrolibrary provides a great RangeSlider control. Here is an example of them from the demo application:
该MahApps.Metro库提供了一个巨大的RangeSlider控制。以下是演示应用程序中的一个示例:


回答by urlreader
Slider bar has a property IsSelectionRangeEnabled. check the sample: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a9eb8697-3ae4-4045-9508-a75d6168a8af/
滑块栏有一个属性 IsSelectionRangeEnabled。检查示例:http: //social.msdn.microsoft.com/Forums/en-US/wpf/thread/a9eb8697-3ae4-4045-9508-a75d6168a8af/
回答by Clint StLaurent
UPDATE: Avalon controls is the older predecessor to the WPF Extended Toolkit, which now includes the AvalonDock.
更新:Avalon 控件是 WPF 扩展工具包的较旧前身,现在包括 AvalonDock。
RangeSlider is part of the WPF Extended toolkit.
RangeSlider 是 WPF 扩展工具包的一部分。
回答by Ony
As @kmatyaszekalready said AvalonControlsLibraryis great free (Microsoft Public License (Ms-PL)) Library of controls.
正如@kmatyaszek已经说过AvalonControlsLibrary是伟大的自由(微软公共许可(MS-PL)的控制)库。
However i found for myself basic styles for RangeSlidernot very satisfy.
然而,我发现自己对RangeSlider 的基本样式不是很满意。
Here is style for more modern version for it(RangeSlider.xaml):
这是更现代版本的样式(RangeSlider.xaml):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:drawing="clr-namespace:System.Drawing;assembly=System.Drawing" >
<SolidColorBrush x:Key="BrushSliderEdge" Color="LightGray" />
<SolidColorBrush x:Key="BrushSliderActiveArea" Color="DeepSkyBlue" />
<SolidColorBrush x:Key="BrushSliderThumb" Color="LightSkyBlue" />
<SolidColorBrush x:Key="BrushSliderThumbBorder" Color="DeepSkyBlue" />
<Style x:Key="SliderEdge" TargetType="RepeatButton">
<Setter Property="Focusable" Value="false" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RepeatButton">
<Border Height="3" Background="{StaticResource BrushSliderEdge}" BorderBrush="{StaticResource BrushSliderEdge}" BorderThickness="1" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SliderInner" TargetType="Thumb">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb">
<Border Height="3" Background="{StaticResource BrushSliderActiveArea}" BorderBrush="{StaticResource BrushSliderThumbBorder}" BorderThickness="1" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SliderThumb" TargetType="Thumb">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb">
<Ellipse Width="10" Height="10" Fill="{StaticResource BrushSliderThumb}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:RangeSlider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:RangeSlider}">
<StackPanel Name="PART_RangeSliderContainer" Orientation="Horizontal">
<RepeatButton Name="PART_LeftEdge" Style="{StaticResource SliderEdge}" />
<Thumb Name="PART_LeftThumb" Cursor="SizeWE" Style="{StaticResource SliderThumb}" />
<Thumb Name="PART_MiddleThumb" MinWidth="10" Cursor="ScrollAll" Style="{StaticResource SliderInner}" />
<Thumb Name="PART_RightThumb" Cursor="SizeWE" Style="{StaticResource SliderThumb}" />
<RepeatButton Name="PART_RightEdge" Style="{StaticResource SliderEdge}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Here is example how it will look (you can change colors by changing brushes color values):
这是它的外观示例(您可以通过更改画笔颜色值来更改颜色):
Replace with it base style from: http://avaloncontrolslib.codeplex.com/SourceControl/latest#trunk/AvalonControlsLibrary/Themes/RangeSlider.xaml
And use for control: http://avaloncontrolslib.codeplex.com/SourceControl/latest#trunk/AvalonControlsLibrary/Controls/RangeSlider.cs
替换为它的基本样式:http: //avaloncontrolslib.codeplex.com/SourceControl/latest#trunk/AvalonControlsLibrary/Themes/RangeSlider.xaml
并用于控制:http: //avaloncontrolslib.codeplex.com/SourceControl/latest#trunk/AvalonControlsLibrary/Controls/RangeSlider.cs
Example of control usage in .NET 4.5
.NET 4.5 中的控件使用示例
<UserControl x:Class="MyProject.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpf="clr-namespace:Library.WPF;assembly=Library"
MinWidth="700"
HorizontalAlignment="Left"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary Source="/Library;component/WPF/RangeSlider/RangeSlider.xaml" />
</UserControl.Resources>
<StackPanel>
<wpf:RangeSlider Width="400"
MinRange="0"
RangeStart="10"
RangeStartSelected="{Binding MyValue_Min}"
RangeStop="100"
RangeStopSelected="{Binding MyValue_Max}" />
</StackPanel>
In above example:
在上面的例子中:
Assembly where control data located: Library
控制数据所在的程序集:库
Namespace: Library.WPF
命名空间:Library.WPF
- /WPF/RangeSlider/RangeSlider.xaml<-- xaml style
- /WPF/RangeSlider/RangeSlider.cs<-- control code
- /WPF/RangeSlider/RangeSlider.xaml<-- xaml 样式
- /WPF/RangeSlider/RangeSlider.cs<-- 控制代码
回答by Michel Keijzers
What i tried is placing two sliders one over he other, so it looks like a range slider Then, you can set the value, one slider for min and another for max,and add the logic for greater and smaller values. Hope it works, in this case you need not add any external third party library
我尝试的是将两个滑块放在另一个上面,所以它看起来像一个范围滑块然后,您可以设置值,一个滑块用于最小值,另一个滑块用于最大值,并添加更大和更小值的逻辑。希望它有效,在这种情况下,您无需添加任何外部第三方库
回答by Jawad Sabir
I would recommend the RangeSlider by Xceed.
我会推荐 Xceed 的 RangeSlider。
https://github.com/xceedsoftware/wpftoolkit/wiki/RangeSlider
https://github.com/xceedsoftware/wpftoolkit/wiki/RangeSlider

