Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProgressBar track height is not overridable #9010

Closed
AndrewKeepCoding opened this issue Oct 24, 2023 · 6 comments
Closed

ProgressBar track height is not overridable #9010

AndrewKeepCoding opened this issue Oct 24, 2023 · 6 comments
Labels
area-Progress ProgressBar, ProgressRing bug Something isn't working needs-triage Issue needs to be triaged by the area owners team-Controls Issue for the Controls team

Comments

@AndrewKeepCoding
Copy link
Contributor

Describe the bug

The ProgressBar track height is not overridable.
For example, the code below should render a ProgresBar with a track that fills (or almost fills) the green border,

<Grid>
    <Grid.Resources>
        <x:Double x:Key="ProgressBarTrackHeight">50</x:Double>
    </Grid.Resources>
    <ProgressBar
        Height="50"
        BorderBrush="LightGreen"
        BorderThickness="1"
        Maximum="100"
        Value="70" />
</Grid>

but renders this instead:
image

Steps to reproduce the bug

Use the code above.

Expected behavior

ProgressBarTrackHeight should be applied to the track height.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.4.2: 1.4.231008000

Windows version

No response

Additional context

This issue was supposed to be fixed by #6061.

@AndrewKeepCoding AndrewKeepCoding added the bug Something isn't working label Oct 24, 2023
@AndrewKeepCoding
Copy link
Contributor Author

AndrewKeepCoding commented Oct 24, 2023

For those who needs a quick workaround until this issue gets fixed, something like this seems to work so far:

public class CustomProgressBar : ProgressBar
{
    private long _heightPropertyChangedCallBackToken;

    public CustomProgressBar () : base()
    {
        Loaded += OnLoaded;
        Unloaded += OnUnloaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        _heightPropertyChangedCallBackToken = RegisterPropertyChangedCallback(HeightProperty, OnHeightPropertyChanged);
        ApplyHeight();
    }

    private void OnUnloaded(object sender, RoutedEventArgs e)
    {
        UnregisterPropertyChangedCallback(HeightProperty, _heightPropertyChangedCallBackToken);
    }

    private void ApplyHeight()
    {
        // FindDescendants() is from the CommunityToolkit.WinUI.Extensions NuGet package.
        foreach (FrameworkElement element in this.FindDescendants()
            .OfType<FrameworkElement>()
            .Where(x => x is Grid or Rectangle))
        {
            element.Height = Height;
        }
    }

    private void OnHeightPropertyChanged(DependencyObject sender, DependencyProperty dp)
    {
        ApplyHeight();
    }
}

@castorix
Copy link

castorix commented Oct 24, 2023

If I add MinHeight ="50", I get (with 1.4.230822000 version) :

image

@AndrewKeepCoding
Copy link
Contributor Author

AndrewKeepCoding commented Oct 24, 2023

That's way simple if you won't change the Height dynamically.

So, we can do:

<Grid>
    <Grid.Resources>
        <x:Double x:Key="ProgressBarTrackHeight">50</x:Double>
    </Grid.Resources>
    <ProgressBar
        MinHeight="{StaticResource ProgressBarTrackHeight}"
        Maximum="100"
        Value="70" />
</Grid>

Thanks!

@bpulliam bpulliam added area-Progress ProgressBar, ProgressRing team-Controls Issue for the Controls team labels Oct 26, 2023
@gegao18
Copy link

gegao18 commented Oct 31, 2023

Thanks for reporting this. Closing issue now that there's a MinHeight workaround.

@gegao18 gegao18 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 31, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jul 22, 2024
@codendone codendone removed the needs-triage Issue needs to be triaged by the area owners label Aug 7, 2024
@FireController1847
Copy link

@gegao18 Not sure if that's really a solid solution here — it's still not possible to change the height of the progress bar dynamically. It seems to me like the workaround doesn't work for this particular case.

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jan 6, 2025
@FireController1847
Copy link

FireController1847 commented Jan 7, 2025

For those who needs a quick workaround until this issue gets fixed, something like this seems to work so far:

public class CustomProgressBar : ProgressBar
{
private long _heightPropertyChangedCallBackToken;

public CustomProgressBar () : base()
{
    Loaded += OnLoaded;
    Unloaded += OnUnloaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
    _heightPropertyChangedCallBackToken = RegisterPropertyChangedCallback(HeightProperty, OnHeightPropertyChanged);
    ApplyHeight();
}

private void OnUnloaded(object sender, RoutedEventArgs e)
{
    UnregisterPropertyChangedCallback(HeightProperty, _heightPropertyChangedCallBackToken);
}

private void ApplyHeight()
{
    // FindDescendants() is from the CommunityToolkit.WinUI.Extensions NuGet package.
    foreach (FrameworkElement element in this.FindDescendants()
        .OfType<FrameworkElement>()
        .Where(x => x is Grid or Rectangle))
    {
        element.Height = Height;
    }
}

private void OnHeightPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
    ApplyHeight();
}

}

It's ugly, but here's the version I got to work on my .NET MAUI project, in case anyone is interested:

using Microsoft.Maui.Handlers;
#if WINDOWS
using CommunityToolkit.WinUI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Shapes;
using Microsoft.UI.Xaml.Controls;
using System.Linq;
#endif

namespace MVVM.Handlers {

    /// <summary>
    /// Used to provide customizations for the ProgressBar.
    /// </summary>
    public class CustomProgressBarHandler : ProgressBarHandler {

#if WINDOWS
        /// <inheritdoc/>
        protected override ProgressBar CreatePlatformView() {
            ProgressBar progressBar = base.CreatePlatformView();

            // Apply height patches
            long callbackToken = 0;

            // Manually searches for sub-elements and updates their associated heights.
            void UpdateHeight() {
                foreach (FrameworkElement element in progressBar.FindDescendants().OfType<FrameworkElement>().Where(x => x is Grid or Rectangle)) {
                    element.Height = progressBar.Height;
                }
            }

            // Called when the <see cref="FrameworkElement.HeightProperty"/> for the progress bar is changed.
            void OnHeightPropertyChanged(DependencyObject sender, DependencyProperty property) {
                UpdateHeight();
            }

            // Attach events
            progressBar.Loaded += (sender, e) => {
                callbackToken = progressBar.RegisterPropertyChangedCallback(ProgressBar.HeightProperty, OnHeightPropertyChanged);

                // Update height to our desired styling
                progressBar.Height = 50;
            };
            progressBar.Unloaded += (sender, e) => {
                progressBar.UnregisterPropertyChangedCallback(ProgressBar.HeightProperty, callbackToken);
            };

            return progressBar;
        }
#endif

    }

}

This avoids the creation of a new class which creates all sorts of weird issues with resources and default styling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Progress ProgressBar, ProgressRing bug Something isn't working needs-triage Issue needs to be triaged by the area owners team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

6 participants