AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.89k stars 2.24k forks source link

ProgressBar throws System.ArgumentException if Padding is set #17393

Open pavelovcharov opened 1 week ago

pavelovcharov commented 1 week ago

Describe the bug

In Avalonia 11.2.0 ProgressBar throws System.ArgumentException if you set the Padding property

System.ArgumentException: -0,2 is not a valid value for 'Width.
   at Avalonia.PropertyStore.ValueStore.SetValue[T](StyledProperty`1 property, T value, BindingPriority priority)
   at Avalonia.AvaloniaObject.SetValue[T](StyledProperty`1 property, T value, BindingPriority priority)
   at Avalonia.Layout.Layoutable.set_Width(Double value)
   at Avalonia.Controls.ProgressBar.UpdateIndicator()
   at Avalonia.Controls.ProgressBar.OnApplyTemplate(TemplateAppliedEventArgs e)
   at Avalonia.Controls.Primitives.TemplatedControl.ApplyTemplate()
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutHelper.MeasureChild(Layoutable control, Size availableSize, Thickness padding, Thickness borderThickness)
   at Avalonia.Controls.Presenters.ContentPresenter.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutHelper.MeasureChild(Layoutable control, Size availableSize, Thickness padding)
   at Avalonia.Controls.Decorator.MeasureOverride(Size availableSize)
   at Avalonia.Controls.Primitives.VisualLayerManager.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureOverride(Size availableSize)
   at Avalonia.Controls.Window.MeasureOverride(Size availableSize)
   at Avalonia.Controls.WindowBase.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutManager.Measure(Layoutable control)
   at Avalonia.Layout.LayoutManager.ExecuteInitialLayoutPass()
   at Avalonia.Controls.Window.ShowCore(Window owner)
   at Avalonia.Controls.Window.Show()
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.ShowMainWindow()
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.StartCore(String[] args)
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, Action`1 lifetimeBuilder)
   at AvaloniaApplication4.Program.Main(String[] args) in E:\work\tests\AvaloniaApplication4\AvaloniaApplication4\Program.cs:line 12

To Reproduce

Add a ProgressBar control to your XAML markup

 <ProgressBar Padding="1" Value="10"></ProgressBar>

Expected behavior

No exception should be thrown

Avalonia version

11.2.0

OS

No response

Additional context

https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/ProgressBar.cs#L348

Looks like the UpdateIndicator method should not be called in the OnApplyTemplate method, because the ProgressBar's visual tree is not measured yet

timunie commented 1 week ago

@pavelovcharov maybe you are right about that finding. If you have an idea how to fix it feel free to open a PR. Mind that template can change in theory while control is already loaded.