Slider
The Slider control allows users to select a value from a continuous range of values by moving a thumb along a track. It's commonly used for adjusting settings like volume, brightness, or progress.
Introduction
The WinUI Slider
control provides an intuitive way for users to select a single numerical value from a defined range. It's a fundamental UI element for scenarios requiring gradual adjustments or selections.
Basic Usage
To use the Slider
, you typically define a range and bind its value to a variable in your application. Here's a simple XAML example:
<Slider Minimum="0" Maximum="100" Value="50" />
Interactive Example
In the example above, you can interact with the sliders. The "Current Value" will update dynamically as you drag the thumb.
JavaScript for Interaction
You can use JavaScript to handle value changes:
document.getElementById('volumeSlider').addEventListener('input', function() {
document.getElementById('volumeValue').textContent = this.value;
});
document.getElementById('brightnessSlider').addEventListener('input', function() {
document.getElementById('brightnessValue').textContent = this.value;
});
Key Properties
The Slider
control offers several properties to customize its behavior and appearance:
Property | Description | Type | Default |
---|---|---|---|
Minimum |
The smallest value allowed in the slider's range. | Double |
0 |
Maximum |
The largest value allowed in the slider's range. | Double |
100 |
Value |
The current value of the slider. This is what the user selects. | Double |
0 |
StepFrequency |
The increment between values when the user moves the thumb. | Double |
1 |
Orientation |
Specifies whether the slider is horizontal or vertical. | Orientation (Horizontal , Vertical ) |
Horizontal |
IsThumbDraggable |
Determines if the thumb can be moved by the user. | Boolean |
True |
Events
The Slider
raises events to notify your application about changes:
Event | Description |
---|---|
ValueChanged |
Occurs when the Value property changes, either by user interaction or programmatically. |
Best Practices
- Ensure the
Minimum
andMaximum
values clearly define the usable range for your scenario. - Use descriptive labels to indicate what the slider controls (e.g., "Volume", "Opacity").
- For precise control, set an appropriate
StepFrequency
. - Consider accessibility by ensuring sufficient contrast and providing alternative input methods if needed.