Add SliderExtensions (mouse-wheel attached properties)#851
Open
niels9001 wants to merge 1 commit intoCommunityToolkit:mainfrom
Open
Add SliderExtensions (mouse-wheel attached properties)#851niels9001 wants to merge 1 commit intoCommunityToolkit:mainfrom
niels9001 wants to merge 1 commit intoCommunityToolkit:mainfrom
Conversation
Adds SliderExtensions.IsMouseWheelEnabled and SliderExtensions.MouseWheelChange attached properties to the Extensions component, allowing Slider controls to respond to mouse-wheel input. Includes a sample (SliderWheelSample) and docs page. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an opt-in SliderExtensions API to enable mouse-wheel interaction for WinUI Slider controls, plus a gallery sample and documentation to demonstrate usage within the Extensions component.
Changes:
- Introduces
SliderExtensions.IsMouseWheelEnabledandSliderExtensions.MouseWheelChangeattached properties to adjustSlider.Valueon wheel input. - Adds a new interactive sample page (
SliderWheelSample) demonstrating the attached properties. - Adds a new docs page describing the new API and linking the sample.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| components/Extensions/src/Slider/SliderExtensions.cs | New attached properties and wheel handler implementation for Slider. |
| components/Extensions/samples/SliderExtensions/SliderWheelSample.xaml.cs | Sample page code-behind wiring the toolkit sample metadata. |
| components/Extensions/samples/SliderExtensions/SliderWheelSample.xaml | Sample UI demonstrating wheel-over-slider behavior. |
| components/Extensions/samples/SliderExtensions.md | Documentation for SliderExtensions and its attached properties. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+19
to
+35
| public static readonly DependencyProperty IsMouseWheelEnabledProperty = DependencyProperty.RegisterAttached( | ||
| nameof(GetIsMouseWheelEnabled)[3..], | ||
| typeof(bool), | ||
| typeof(SliderExtensions), | ||
| new PropertyMetadata(false, OnIsMouseWheelEnabledChanged)); | ||
|
|
||
| /// <summary> | ||
| /// Attached <see cref="DependencyProperty"/> for the value added to or subtracted | ||
| /// from <see cref="RangeBase.Value"/> per mouse-wheel notch. Defaults to | ||
| /// <see cref="double.NaN"/>, which means "use the slider's own | ||
| /// <see cref="RangeBase.SmallChange"/>". | ||
| /// </summary> | ||
| public static readonly DependencyProperty MouseWheelChangeProperty = DependencyProperty.RegisterAttached( | ||
| nameof(GetMouseWheelChange)[3..], | ||
| typeof(double), | ||
| typeof(SliderExtensions), | ||
| new PropertyMetadata(double.NaN)); |
| } | ||
|
|
||
| double step = GetMouseWheelChange(slider); | ||
| if (double.IsNaN(step) || step <= 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new
SliderExtensionsstatic class to the Extensions component, enabling mouse-wheel input onSlidercontrols via two attached properties:SliderExtensions.IsMouseWheelEnabledboolfalsetrue, wheel events on the slider adjustValue.SliderExtensions.MouseWheelChangedoubleNaN(use slider's ownSmallChange)Value.Why
By default
Sliderignores the mouse wheel. This is a small, common, opt-in convenience that's easy to add without subclassing — particularly useful when sliders are placed inside aScrollViewerand you want wheel-over-slider to adjust the value rather than scroll the surrounding container (the handler marks the wheel event handled).Behavior
notches = MouseWheelDelta / 120and appliesnotches * step, clamped to[Minimum, Maximum].MouseWheelChangedefaults toNaN, in which case the slider's ownSmallChangeis used as the per-notch delta — same source of truth used by arrow keys, no second knob unless the author wants one.e.Handled = trueso an enclosingScrollViewerdoesn't also scroll.PointerWheelChangedbased on theIsMouseWheelEnabledvalue, so toggling it off cleanly removes the handler.Origin
This was first written for the PowerToys PowerDisplay module, where wheel-scrollable brightness/contrast/volume sliders are very desirable. The implementation is intentionally dependency-free so it can live in the Toolkit and replace the local copy.
Files
components/Extensions/src/Slider/SliderExtensions.cs— the extension.components/Extensions/samples/SliderExtensions/SliderWheelSample.xaml(.cs)— interactive sample with toggleable bool/numeric options.components/Extensions/samples/SliderExtensions.md— docs page.Validation
CommunityToolkit.WinUI.Extensionsfornet8.0-windows10.0.19041.0andnet9.0-windows10.0.19041.0— clean.Extensions.Wasdkgallery head: wheel-over-slider adjusts the value, togglingIsMouseWheelEnabledoff restores default behavior,MouseWheelChangenumeric option changes the per-notch step.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com