XAML 如何在WinUI 3中更改ProgressBarTrackHeight高度?

qojgxg4l  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(90)

我想在WinUI-3中更改进度条的ProgressBarTrackHeight的高度。我不知道如何做,请用代码示例帮助我。

<ProgressBar Width="130" Value="30" />

字符串
x1c 0d1x的数据

ih99xse1

ih99xse11#

在GitHub repo中有一个关于这个的问题。
作为一种解决方法,在问题得到解决之前,您可以创建一个自定义的ProgressBar

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()
    {
        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();
    }
}

字符串
现在Height属性应该可以工作了:

<local:CustomProgressBar
    Height="50"
    Width="130"
    Value="30" />


请注意,FindDescendants来自CommunityToolkit.WinUI.Extensions NuGet包。

更新

在GitHub repo中,针对您不会动态更改Height的情况,建议了一个更好的选择。您只需设置MinHeight,而不是创建自定义ProgressBar

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

相关问题