feat: add property Size.

This commit is contained in:
Zhang Dian
2024-06-07 18:09:36 +08:00
parent 8c53aea890
commit 9a79d7213c
6 changed files with 46 additions and 7 deletions

View File

@@ -5,11 +5,13 @@
<Setter Property="Character" Value="{DynamicResource RatingStarIconGlyph}" />
<Setter Property="Background" Value="{DynamicResource RatingCharacterBackground}" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Margin" Value="3,4" />
<Setter Property="Margin" Value="{DynamicResource RatingCharacterMargin}" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Template">
<ControlTemplate TargetType="u:RatingCharacter">
<Canvas Name="PART_Root" Width="24" Height="24">
<Canvas Name="PART_Root"
Width="{TemplateBinding Size}"
Height="{TemplateBinding Size}">
<Path Width="{Binding #PART_Root.Width}"
Height="{Binding #PART_Root.Height}"
Stretch="Uniform"
@@ -39,6 +41,7 @@
<ControlTheme x:Key="{x:Type u:Rating}" TargetType="u:Rating">
<Setter Property="Foreground" Value="{DynamicResource RatingCharacterForeground}" />
<Setter Property="Character" Value="{DynamicResource RatingStarIconGlyph}" />
<Setter Property="Size" Value="{DynamicResource RatingDefaultSize}" />
<Setter Property="ItemTemplate">
<DataTemplate>
<u:RatingCharacter />
@@ -68,5 +71,8 @@
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^.Small">
<Setter Property="Size" Value="{DynamicResource RatingSmallSize}" />
</Style>
</ControlTheme>
</ResourceDictionary>

View File

@@ -1,3 +1,6 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Double x:Key="RatingDefaultSize">24</x:Double>
<x:Double x:Key="RatingSmallSize">16</x:Double>
<Thickness x:Key="RatingCharacterMargin">3 4</Thickness>
<StreamGeometry x:Key="RatingStarIconGlyph">M10.7525 1.90411C11.1451 0.698628 12.8549 0.698631 13.2475 1.90411L15.2395 8.01946H21.6858C22.9565 8.01946 23.4848 9.64143 22.4568 10.3865L17.2417 14.1659L19.2337 20.2813C19.6263 21.4868 18.2431 22.4892 17.2151 21.7442L12 17.9647L6.78489 21.7442C5.75687 22.4892 4.37368 21.4868 4.76635 20.2813L6.75834 14.1659L1.54323 10.3865C0.515206 9.64142 1.04354 8.01946 2.31425 8.01946H8.76048L10.7525 1.90411Z</StreamGeometry>
</ResourceDictionary>

View File

@@ -38,6 +38,9 @@ public class Rating : TemplatedControl
public static readonly StyledProperty<double> DefaultValueProperty =
AvaloniaProperty.Register<Rating, double>(nameof(DefaultValue));
public static readonly StyledProperty<double> SizeProperty =
AvaloniaProperty.Register<RatingCharacter, double>(nameof(Size));
public static readonly StyledProperty<IDataTemplate?> ItemTemplateProperty =
AvaloniaProperty.Register<Rating, IDataTemplate?>(nameof(ItemTemplate));
@@ -83,6 +86,12 @@ public class Rating : TemplatedControl
set => SetValue(DefaultValueProperty, value);
}
public double Size
{
get => GetValue(SizeProperty);
set => SetValue(SizeProperty, value);
}
public IDataTemplate? ItemTemplate
{
get => GetValue(ItemTemplateProperty);
@@ -137,7 +146,8 @@ public class Rating : TemplatedControl
Items.Add(new RatingCharacter
{
Character = Character,
AllowHalf = AllowHalf
AllowHalf = AllowHalf,
Size = Size,
});
}
}
@@ -174,7 +184,8 @@ public class Rating : TemplatedControl
Items.Add(new RatingCharacter
{
Character = Character,
AllowHalf = AllowHalf
AllowHalf = AllowHalf,
Size = Size,
}
);
}

View File

@@ -23,6 +23,9 @@ public class RatingCharacter : TemplatedControl
public static readonly StyledProperty<object> CharacterProperty =
Rating.CharacterProperty.AddOwner<RatingCharacter>();
public static readonly StyledProperty<double> SizeProperty =
Rating.SizeProperty.AddOwner<RatingCharacter>();
public bool AllowHalf
{
get => GetValue(AllowHalfProperty);
@@ -35,6 +38,12 @@ public class RatingCharacter : TemplatedControl
set => SetValue(CharacterProperty, value);
}
public double Size
{
get => GetValue(SizeProperty);
set => SetValue(SizeProperty, value);
}
internal bool IsLast { get; set; }
private bool _isHalf;