feat: add property Character.

This commit is contained in:
Zhang Dian
2024-06-07 10:52:35 +08:00
parent e5628f7f23
commit 34568a4af8
5 changed files with 48 additions and 25 deletions

View File

@@ -10,19 +10,32 @@
x:CompileBindings="True"
x:DataType="vm:RatingDemoViewModel"
mc:Ignorable="d">
<UserControl.Resources>
<StreamGeometry x:Key="LoveHeartGeometry">
M12 5.99999C10.5 2.12432 5.75193 2.0557 3.40383 4.4038C0.865423 6.94221 0.999999 10.5 3.50001 14C5.52139 16.8299 9.83088 20.3136 11.4069 21.5438C11.7573 21.8172 12.2427 21.8172 12.5931 21.5438C14.1691 20.3136 18.4786 16.8299 20.5 14C23 10.5 23.1346 6.94221 20.5962 4.4038C18.2481 2.0557 14.5 2.12432 12 5.99999Z
</StreamGeometry>
</UserControl.Resources>
<StackPanel Spacing="20">
<Grid ColumnDefinitions="*, 300">
<Grid Grid.Column="0">
<StackPanel>
<StackPanel HorizontalAlignment="Center">
<u:Rating
HorizontalAlignment="Center"
AllowClear="{Binding AllowClear }"
AllowHalf="{Binding AllowHalf }"
AllowFocus="{Binding AllowFocus }"
AllowClear="{Binding AllowClear}"
AllowHalf="{Binding AllowHalf}"
AllowFocus="{Binding AllowFocus}"
IsEnabled="{Binding IsEnabled}"
Value="{Binding Value}"
Count="{Binding Count}"
DefaultValue="{Binding DefaultValue }" />
DefaultValue="{Binding DefaultValue}" />
<u:Rating
AllowClear="{Binding AllowClear}"
AllowHalf="{Binding AllowHalf}"
AllowFocus="{Binding AllowFocus}"
IsEnabled="{Binding IsEnabled}"
Value="{Binding Value}"
Count="{Binding Count}"
DefaultValue="{Binding DefaultValue}"
Character="{StaticResource LoveHeartGeometry}" />
</StackPanel>
</Grid>
<Border Grid.Column="1" VerticalAlignment="Top">

View File

@@ -2,7 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
<ControlTheme x:Key="{x:Type u:RatingCharacter}" TargetType="u:RatingCharacter">
<Setter Property="Foreground" Value="{DynamicResource RatingCharacterUnSelectedForeground}" />
<Setter Property="Character" Value="{DynamicResource RatingStarIconGlyph}" />
<Setter Property="Background" Value="{DynamicResource RatingCharacterBackground}" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="VerticalAlignment" Value="Stretch" />
@@ -12,14 +12,14 @@
<Canvas Width="24" Height="24">
<Path Width="24"
Height="24"
Data="{DynamicResource RatingStarIconGlyph}"
Data="{TemplateBinding Character}"
Fill="{TemplateBinding Background}" />
<Border Name="{x:Static u:RatingCharacter.PART_IconGlyph}"
ClipToBounds="True">
<Path Width="24"
Height="24"
HorizontalAlignment="Left"
Data="{DynamicResource RatingStarIconGlyph}"
Data="{TemplateBinding Character}"
Fill="{TemplateBinding Foreground}" />
</Border>
</Canvas>
@@ -36,6 +36,8 @@
</ControlTheme>
<ControlTheme x:Key="{x:Type u:Rating}" TargetType="u:Rating">
<Setter Property="Foreground" Value="{DynamicResource RatingCharacterUnSelectedForeground}" />
<Setter Property="Character" Value="{DynamicResource RatingStarIconGlyph}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="ItemTemplate">
<DataTemplate>
@@ -45,8 +47,6 @@
<Setter Property="Template">
<ControlTemplate TargetType="u:Rating">
<Border Name="PART_RootBorder"
MinHeight="30"
Padding="8,4"
VerticalAlignment="Stretch"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"

View File

@@ -1,3 +1,3 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<PathGeometry 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</PathGeometry>
<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

@@ -115,6 +115,7 @@ public class Rating : TemplatedControl
private void OnValueChanged(AvaloniaPropertyChangedEventArgs e)
{
if (!IsLoaded) return;
if (e.NewValue is double newValue)
{
UpdateItemsByValue(newValue);
@@ -133,7 +134,11 @@ public class Rating : TemplatedControl
var itemsToAdd = newCount - currentCount;
for (var i = 0; i < itemsToAdd; i++)
{
Items.Add(new RatingCharacter());
Items.Add(new RatingCharacter
{
Character = Character,
AllowHalf = AllowHalf
});
}
}
else if (currentCount > newCount)
@@ -145,11 +150,6 @@ public class Rating : TemplatedControl
}
}
foreach (var item in Items)
{
item.AllowHalf = AllowHalf;
}
UpdateItemsByValue(Value);
}
@@ -171,26 +171,27 @@ public class Rating : TemplatedControl
_itemsControl = e.NameScope.Find<ItemsControl>(PART_ItemsControl);
for (var i = 0; i < Count; i++)
{
Items.Add(new RatingCharacter());
}
foreach (var item in Items)
{
item.AllowHalf = AllowHalf;
Items.Add(new RatingCharacter
{
Character = Character,
AllowHalf = AllowHalf
}
);
}
SetCurrentValue(ValueProperty, DefaultValue);
UpdateItemsByValue(DefaultValue);
}
internal void PointerEnteredHandler(RatingCharacter o)
{
var index = Items.IndexOf(o);
var item = Items.FirstOrDefault(item => item.IsLast);
if (item is not null)
{
item.IsHalf = false;
}
var index = Items.IndexOf(o);
UpdateItemsByIndex(index);
}

View File

@@ -20,12 +20,21 @@ public class RatingCharacter : TemplatedControl
public static readonly StyledProperty<bool> AllowHalfProperty =
Rating.AllowHalfProperty.AddOwner<RatingCharacter>();
public static readonly StyledProperty<object> CharacterProperty =
Rating.CharacterProperty.AddOwner<RatingCharacter>();
public bool AllowHalf
{
get => GetValue(AllowHalfProperty);
set => SetValue(AllowHalfProperty, value);
}
public object Character
{
get => GetValue(CharacterProperty);
set => SetValue(CharacterProperty, value);
}
internal bool IsLast { get; set; }
private bool _isHalf;