feat: DualBadge CornerRadius.

This commit is contained in:
Zhang Dian
2023-07-27 10:22:50 +08:00
parent f429d8219c
commit c5589b3db3
3 changed files with 87 additions and 28 deletions

View File

@@ -19,40 +19,38 @@
<ControlTemplate TargetType="{x:Type u:DualBadge}">
<Border
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}">
VerticalAlignment="{TemplateBinding VerticalAlignment}"
ClipToBounds="True"
CornerRadius="{TemplateBinding CornerRadius}">
<Grid ColumnDefinitions="*,*">
<Border
<DockPanel
Grid.Column="0"
Background="{TemplateBinding HeaderBackground}"
CornerRadius="{DynamicResource DualBadgeDefaultLeftCornerRadius}">
<DockPanel>
<ContentPresenter
Name="{x:Static u:DualBadge.PART_Icon}"
Width="14"
Height="14"
Margin="2 0 0 0"
IsVisible="{TemplateBinding Icon,Converter={x:Static ObjectConverters.IsNotNull}}"
Foreground="{TemplateBinding IconForeground}"
Padding="{TemplateBinding Padding}"
Content="{TemplateBinding Icon}"
ContentTemplate="{TemplateBinding IconTemplate}" />
<ContentPresenter
Name="{x:Static u:DualBadge.PART_HeaderPresenter}"
TextElement.FontSize="{TemplateBinding FontSize}"
IsVisible="{TemplateBinding Header,Converter={x:Static ObjectConverters.IsNotNull}}"
Foreground="{TemplateBinding HeaderForeground}"
Padding="{TemplateBinding Padding}"
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}" />
</DockPanel>
</Border>
Background="{TemplateBinding HeaderBackground}">
<ContentPresenter
Name="{x:Static u:DualBadge.PART_Icon}"
Width="14"
Height="14"
Margin="2 0 0 0"
IsVisible="{TemplateBinding Icon,Converter={x:Static ObjectConverters.IsNotNull}}"
Foreground="{TemplateBinding IconForeground}"
Padding="{TemplateBinding Padding}"
Content="{TemplateBinding Icon}"
ContentTemplate="{TemplateBinding IconTemplate}" />
<ContentPresenter
Name="{x:Static u:DualBadge.PART_HeaderPresenter}"
TextElement.FontSize="{TemplateBinding FontSize}"
IsVisible="{TemplateBinding Header,Converter={x:Static ObjectConverters.IsNotNull}}"
Foreground="{TemplateBinding HeaderForeground}"
Padding="{TemplateBinding Padding}"
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"/>
</DockPanel>
<ContentPresenter
Name="{x:Static u:DualBadge.PART_ContentPresenter}"
Grid.Column="1"
TextElement.FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}"
Padding="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
@@ -60,5 +58,9 @@
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:header-empty">
<Setter Property="HeaderBackground" Value="LightGreen" />
</Style>
</ControlTheme>
</ResourceDictionary>

View File

@@ -1,6 +1,5 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<CornerRadius x:Key="DualBadgeDefaultLeftCornerRadius">4 0 0 4</CornerRadius>
<CornerRadius x:Key="DualBadgeDefaultCornerRadius">0 4 4 0</CornerRadius>
<CornerRadius x:Key="DualBadgeDefaultCornerRadius">4</CornerRadius>
<x:Double x:Key="DualBadgeDefaultFontSize">12</x:Double>
<Thickness x:Key="DualBadgeDefaultThickness">1</Thickness>
<Thickness x:Key="DualBadgeDefaultPadding">4 2</Thickness>

View File

@@ -1,4 +1,5 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
@@ -7,9 +8,13 @@ using Avalonia.Media;
namespace Ursa.Controls;
[PseudoClasses(PC_IconEmpty, PC_HeaderEmpty, PC_ContentEmpty)]
[TemplatePart(PART_Icon, typeof(ContentPresenter))]
public class DualBadge : HeaderedContentControl
{
public const string PC_IconEmpty = ":icon-empty";
public const string PC_HeaderEmpty = ":header-empty";
public const string PC_ContentEmpty = ":content-empty";
public const string PART_HeaderPresenter = "PART_HeaderPresenter";
public const string PART_ContentPresenter = "PART_ContentPresenter";
public const string PART_Icon = "PART_Icon";
@@ -58,4 +63,57 @@ public class DualBadge : HeaderedContentControl
get => GetValue(HeaderBackgroundProperty);
set => SetValue(HeaderBackgroundProperty, value);
}
public static readonly StyledProperty<bool> IsIconEmptyProperty = AvaloniaProperty.Register<DualBadge, bool>(
nameof(IsIconEmpty));
public bool IsIconEmpty
{
get => GetValue(IsIconEmptyProperty);
set => SetValue(IsIconEmptyProperty, value);
}
public static readonly StyledProperty<bool> IsHeaderEmptyProperty = AvaloniaProperty.Register<DualBadge, bool>(
nameof(IsHeaderEmpty));
public bool IsHeaderEmpty
{
get => GetValue(IsHeaderEmptyProperty);
set => SetValue(IsHeaderEmptyProperty, value);
}
public static readonly StyledProperty<bool> IsContentEmptyProperty = AvaloniaProperty.Register<DualBadge, bool>(
nameof(IsContentEmpty));
public bool IsContentEmpty
{
get => GetValue(IsContentEmptyProperty);
set => SetValue(IsContentEmptyProperty, value);
}
static DualBadge()
{
IsIconEmptyProperty.Changed.AddClassHandler<DualBadge>((o, e) => o.OnIsIconEmptyChanged(e));
IsHeaderEmptyProperty.Changed.AddClassHandler<DualBadge>((o, e) => o.OnIsHeaderEmptyChanged(e));
IsContentEmptyProperty.Changed.AddClassHandler<DualBadge>((o, e) => o.OnIsContentEmptyChanged(e));
}
private void OnIsIconEmptyChanged(AvaloniaPropertyChangedEventArgs args)
{
bool newValue = args.GetNewValue<bool>();
PseudoClasses.Set(PC_IconEmpty, newValue);
}
private void OnIsHeaderEmptyChanged(AvaloniaPropertyChangedEventArgs args)
{
bool newValue = args.GetNewValue<bool>();
PseudoClasses.Set(PC_HeaderEmpty, newValue);
}
private void OnIsContentEmptyChanged(AvaloniaPropertyChangedEventArgs args)
{
bool newValue = args.GetNewValue<bool>();
PseudoClasses.Set(PC_ContentEmpty, newValue);
}
}