Merge pull request #655 from irihitech/badge

Fix various badge layout issues.
This commit is contained in:
Zhang Dian
2025-04-16 17:00:32 +08:00
committed by GitHub
4 changed files with 39 additions and 8 deletions

View File

@@ -5,6 +5,9 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
xmlns:viewModels="clr-namespace:Ursa.Demo.ViewModels"
xmlns:common="clr-namespace:Ursa.Common;assembly=Ursa"
x:DataType="viewModels:BadgeDemoViewModel"
d:DesignHeight="850"
d:DesignWidth="850"
mc:Ignorable="d">
@@ -20,6 +23,15 @@
</Style>
</StackPanel.Styles>
<Border Theme="{DynamicResource CardBorder}">
<StackPanel>
<u:EnumSelector Name="position" EnumType="{x:Type common:CornerPosition}"/>
<u:Badge CornerPosition="{Binding #position.Value}" Header="{Binding Text}">
<Button Content="Test" Command="{Binding ChangeTextCommand}"/>
</u:Badge>
</StackPanel>
</Border>
<StackPanel Orientation="Horizontal">
<u:Badge Header="5">
<u:Avatar Classes="Square Blue" Content="BM" />

View File

@@ -9,9 +9,4 @@ public partial class BadgeDemo : UserControl
{
InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}

View File

@@ -1,6 +1,28 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Ursa.Demo.ViewModels;
public class BadgeDemoViewModel: ViewModelBase
public partial class BadgeDemoViewModel: ViewModelBase
{
[ObservableProperty] private string? _text = null;
public BadgeDemoViewModel()
{
}
[RelayCommand]
public void ChangeText()
{
if (Text == null)
{
Text = DateTime.Now.ToShortDateString();
}
else
{
Text = null;
}
}
}

View File

@@ -66,6 +66,7 @@ public class Badge : HeaderedContentControl
static Badge()
{
HeaderProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
CornerPositionProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
}
@@ -83,8 +84,9 @@ public class Badge : HeaderedContentControl
protected override Size ArrangeOverride(Size finalSize)
{
var size = base.ArrangeOverride(finalSize);
UpdateBadgePosition();
return base.ArrangeOverride(finalSize);
return size;
}
private void UpdateBadgePosition()