UpdateBadgePosition when the size of _badgeContainer changes
This commit is contained in:
@@ -2,7 +2,6 @@ using Avalonia;
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.Metadata;
|
using Avalonia.Controls.Metadata;
|
||||||
using Avalonia.Controls.Primitives;
|
using Avalonia.Controls.Primitives;
|
||||||
using Avalonia.Interactivity;
|
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
using Avalonia.Styling;
|
using Avalonia.Styling;
|
||||||
using Ursa.Common;
|
using Ursa.Common;
|
||||||
@@ -67,7 +66,6 @@ public class Badge : HeaderedContentControl
|
|||||||
{
|
{
|
||||||
CornerPositionProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
CornerPositionProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
||||||
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
||||||
AffectsArrange<Badge>(HeaderProperty, HeaderTemplateProperty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
@@ -76,19 +74,21 @@ public class Badge : HeaderedContentControl
|
|||||||
_badgeContainer = e.NameScope.Find<Border>(PART_BadgeContainer);
|
_badgeContainer = e.NameScope.Find<Border>(PART_BadgeContainer);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnLoaded(RoutedEventArgs e)
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnLoaded(e);
|
base.OnAttachedToVisualTree(e);
|
||||||
UpdateBadgePosition();
|
ApplyTemplate();
|
||||||
|
_badgeContainer?.AddHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Size ArrangeOverride(Size finalSize)
|
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
var size = base.ArrangeOverride(finalSize);
|
_badgeContainer?.RemoveHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
||||||
UpdateBadgePosition();
|
base.OnDetachedFromVisualTree(e);
|
||||||
return size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnBadgeSizeChanged(object? sender, SizeChangedEventArgs e) => UpdateBadgePosition();
|
||||||
|
|
||||||
private void UpdateBadgePosition()
|
private void UpdateBadgePosition()
|
||||||
{
|
{
|
||||||
var vertical = CornerPosition is CornerPosition.BottomLeft or CornerPosition.BottomRight ? 1 : -1;
|
var vertical = CornerPosition is CornerPosition.BottomLeft or CornerPosition.BottomRight ? 1 : -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user