TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to WPF

WPF Slider Example (ValueChanged)

Use the WPF Slider control with ValueChanged, Value and Maximum.
Slider. A Slider has a minimum and a maximum. It has a button that a user can drag back and forth. And in our C# code, we use the ValueChanged event handler to determine how a slider is being used.
Please begin by creating a WPF project and dragging a Slider to the window. We use the ValueChanged attribute in the XAML. Type "ValueChanged" and press tab. A new event handler in C# code will be created.
In the event handler, we can access the Slider object from the sender. An alternative is to use the RoutedPropertyChangedEventArgs (this is not shown). In the code, we access Value to get a double.

Value: This returns a double variable. It can be any value between Minimum (default 0) and Maximum (default 10).

Double

Tip: Try setting the Minimum and Maximum properties in your XAML code. These properties can also be set in a C# event handler.

Example markup: XAML <Window x:Class="WpfApplication13.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Slider HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="250" ValueChanged="Slider_ValueChanged"/> </Grid> </Window> Example code: C# using System.Windows; using System.Windows.Controls; namespace WpfApplication13 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { // ... Get Slider reference. var slider = sender as Slider; // ... Get Value. double value = slider.Value; // ... Set Window Title. this.Title = "Value: " + value.ToString("0.0") + "/" + slider.Maximum; } } }
When we use the Slider in this program, the Window Title is changed to read "Value: 3.9/10" or something similar. It updates instantly as the Slider is dragged or clicked upon by the user.
Discussion. A Slider is by default horizontal. But in WPF programs, we can rotate the slider in any direction. Simply use the corner boxes on the control in the Visual Studio designer. This can yield a vertical, or even diagonal slider.

Also: Often the Minimum and Maximum attributes need to be adjusted. This can be done in the XAML, or a Slider_Loaded event handler.

Summary. This basic example used the Slider control in a WPF program. It used C# code in the Slider_ValueChanged event handler to dynamically apply changes the user interface, as sliding occurred.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf