- WPF Tutorial
- WPF - Home
- WPF - Overview
- WPF - Environment Setup
- WPF - Hello World
- WPF - XAML Overview
- WPF - Elements Tree
- WPF - Dependency Properties
- WPF - Routed Events
- WPF - Controls
- WPF - Layouts
- WPF - Nesting Of Layout
- WPF - Input
- WPF - Command Line
- WPF - Data Binding
- WPF - Resources
- WPF - Templates
- WPF - Styles
- WPF - Triggers
- WPF - Debugging
- WPF - Custom Controls
- WPF - Exception Handling
- WPF - Localization
- WPF - Interaction
- WPF - 2D Graphics
- WPF - 3D Graphics
- WPF - Multimedia
- WPF Useful Resources
- WPF - Quick Guide
- WPF - Useful Resources
- WPF - Discussion
WPF - Slider
A slider is a control with the help of which a user can select from a range of values by moving a Thumb control along a track. The hierarchical inheritance of Slider class is as follows −
Commonly Used Properties of Slider
Sr. No. | Property & Description |
---|---|
1 | Header Gets or sets the content for the control's header. |
2 | HeaderProperty Identifies the Header dependency property. |
3 | HeaderTemplate Gets or sets the DataTemplate used to display the content of the control's header. |
4 | HeaderTemplateProperty Identifies the HeaderTemplate dependency property. |
5 | IntermediateValue Gets or sets the value of the Slider while the user is interacting with it, before the value is snapped to either the tick or step value. The value the Slider snaps to is specified by the SnapsTo property. |
6 | IntermediateValueProperty Identifies the IntermediateValue dependency property. |
7 | IsDirectionReversed Gets or sets a value that indicates the direction of increasing value. |
8 | IsDirectionReversedProperty Identifies the IsDirectionReversed dependency property. |
9 | IsThumbToolTipEnabled Gets or sets a value that determines whether the slider value is shown in a tool tip for the Thumb component of the Slider. |
10 | IsThumbToolTipEnabledProperty Identifies the IsThumbToolTipEnabled dependency property. |
11 | Orientation Gets or sets the orientation of a Slider. |
12 | OrientationProperty Identifies the Orientation dependency property. |
13 | StepFrequency Gets or sets the value part of a value range that steps should be created for. |
14 | StepFrequencyProperty Identifies the StepFrequency dependency property. |
15 | ThumbToolTipValueConverter Gets or sets the converter logic that converts the range value of the Slider into tool tip content. |
16 | ThumbToolTipValueConverterProperty Identifies the ThumbToolTipValueConverter dependency property. |
17 | TickFrequency Gets or sets the increment of the value range that ticks should be created for. |
18 | TickFrequencyProperty Identifies the TickFrequency dependency property. |
19 | TickPlacement Gets or sets a value that indicates where to draw tick marks in relation to the track. |
20 | TickPlacementProperty Identifies the TickPlacement dependency property. |
Commonly Used Events in Slider Class
Sr. No. | Event & Description |
---|---|
1 | ManipulationCompleted Occurs when a manipulation on the UIElement is complete. (Inherited from UIElement) |
2 | ManipulationDelta Occurs when the input device changes position during a manipulation. (Inherited from UIElement) |
3 | ManipulationInertiaStarting Occurs when the input device loses contact with the UIElement object during a manipulation and inertia begins. (Inherited from UIElement) |
4 | ManipulationStarted Occurs when an input device begins a manipulation on the UIElement. (Inherited from UIElement) |
5 | ManipulationStarting Occurs when the manipulation processor is first created. (Inherited from UIElement) |
6 | ValueChanged Occurs when the range value changes. (Inherited from RangeBase) |
Commonly Used Methods in Slider Class
Sr. No. | Method & Description |
---|---|
1 | OnManipulationCompleted Called before the ManipulationCompleted event occurs. (Inherited from Control) |
2 | OnManipulationDelta Called before the ManipulationDelta event occurs. (Inherited from Control) |
3 | OnManipulationInertiaStarting Called before the ManipulationInertiaStarting event occurs. (Inherited from Control) |
4 | OnManipulationStarted Called before the ManipulationStarted event occurs. (Inherited from Control) |
5 | OnManipulationStarting Called before the ManipulationStarting event occurs. (Inherited from Control) |
6 | OnMaximumChanged Called when the Maximum property changes. (Inherited from RangeBase) |
7 | OnMinimumChanged Called when the Minimum property changes. (Inherited from RangeBase) |
8 | OnValueChanged Fires the ValueChanged routed event. (Inherited from RangeBase) |
9 | SetBinding Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement) |
10 | SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject) |
Example
Let’s create a new WPF project with the name WPFDialog.
Drag one slider and two text blocks from the Toolbox.
Change the background color from the properties window.
The following example shows the usage of Slider in an XAML application. The following XAML code creates a Slider and text blocks and initializes them with some properties and events.
<Window x:Class = "WPFSliderControl.MainWindow" 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:local = "clr-namespace:WPFSliderControl" mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> <StackPanel> <TextBlock Text = "Slider" Margin = "10" /> <Slider x:Name = "slider2" Minimum = "0" Maximum = "100" TickFrequency = "2" TickPlacement = "BottomRight" ValueChanged = "slider2_ValueChanged" Margin = "10"> <Slider.Background> <LinearGradientBrush EndPoint = "0.5,1" StartPoint = "0.5,0"> <GradientStop Color = "Black" Offset = "0" /> <GradientStop Color = "#FFF5DCDC" Offset = "1" /> </LinearGradientBrush> </Slider.Background> </Slider> <TextBlock x:Name = "textBlock1" Margin = "10" Text = "Current value: 0" /> </StackPanel> </Window>
Here is the implementation in C# for ValueChanged event.
using System; using System.Windows; namespace WPFSliderControl { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { int val = Convert.ToInt32(e.NewValue); string msg = String.Format("Current value: {0}", val); this.textBlock1.Text = msg; } } }
When you compile and execute the above code, it will produce the following output −
We recommend that you execute the above example code and try the other properties and events of Slider class.