Skip to content

Added public explicit refresh method to GradientSlider#798

Draft
Avid29 wants to merge 1 commit intoCommunityToolkit:mainfrom
Avid29:gradient-slider/refresh
Draft

Added public explicit refresh method to GradientSlider#798
Avid29 wants to merge 1 commit intoCommunityToolkit:mainfrom
Avid29:gradient-slider/refresh

Conversation

@Avid29
Copy link
Copy Markdown
Member

@Avid29 Avid29 commented Apr 29, 2026

This PR introduces a rather brute-force "solution" to issue #796.

A new method Refresh is added to the GradientSlider which allows the developer to inform the control that the GradientStopCollection has been modified externally.

@Avid29 Avid29 marked this pull request as draft April 29, 2026 16:09
@michael-hawker
Copy link
Copy Markdown
Member

@Avid29 if you make this an ObservableCollection does that change anything? That's the crux of the problem. I don't remember if that always will work with how the XAML compiler initializes the collections in all scenarios though. Especially if the dev does a GradientStopCollection in code-behind and assigns it later.

GradientStops =
[
new GradientStop
{
Color = Colors.Black,
Offset = 0,
},
new GradientStop
{
Color = Colors.White,
Offset = 1,
}
];

Would it be nicer to just have an AddStop and RemoveStop method on the control to have it be a passthru that can then have it update?

@Avid29
Copy link
Copy Markdown
Member Author

Avid29 commented May 1, 2026

@michael-hawker I'm not sure I understand what you mean. That code is using collection initialization syntax to initialize a GradientStopCollection, which is its own concrete type and the type for the main input/output property of the control. It cannot be simply replaced with an ObservableCollection.

However, whether or not using Microsoft.UI.Xaml.Media.GradientStopCollection for the main data property is right to use at all is a valuable discussion. Here's a pros and cons list off the top of my head:

Pros Cons
Is a pre-existing type Does not have a collection updated event
Can bind the control to other GradientBrushes in the Visual Tree Uses Microsoft.UI.** type for data (which is weird to put in the ViewModel for MVVM)

Using a Microsoft.UI.** type for data bothers me particularly. I like to fully separate my View, ViewModel, and Model into separate projects where only the View has any UI package references. Currently there's no solution which makes that feasible.

I think my perspective is to leave as is (with this change) for now, and look at something using a System.Numerics.** color when we get to .NET 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants