feat: move readonly to item.

This commit is contained in:
rabbitism
2024-03-05 17:53:16 +08:00
parent fdc6b2e156
commit 8ac608505b
5 changed files with 25 additions and 71 deletions

View File

@@ -19,6 +19,7 @@
<u:Breadcrumb>
<TextBlock Text="Hello" />
<u:BreadcrumbItem Content="World" IsReadOnly="True"></u:BreadcrumbItem>
<u:BreadcrumbItem Icon="?" Separator="♥" />
<TextBlock Text="Avalonia" />
<TextBlock Text="Ursa" />
@@ -29,7 +30,7 @@
<TextBlock Text="Avalonia" />
<TextBlock Text="Ursa" />
</u:Breadcrumb>
<u:Breadcrumb IsReadOnly="True">
<u:Breadcrumb>
<TextBlock Text="Hello" />
<u:BreadcrumbItem Icon="?" Separator="♥" />
<TextBlock Text="Avalonia" />
@@ -40,6 +41,11 @@
IconBinding="{Binding Icon}"
CommandBinding="{Binding Command}"
ItemsSource="{Binding Items1}">
<u:Breadcrumb.Styles>
<Style Selector="u|BreadcrumbItem" x:DataType="vm:BreadcrumbDemoItem">
<Setter Property="IsReadOnly" Value="{Binding IsReadOnly}"/>
</Style>
</u:Breadcrumb.Styles>
<u:Breadcrumb.IconTemplate>
<DataTemplate DataType="x:String">
<PathIcon Width="12" Height="12" Data="{Binding Path=., Converter={StaticResource IconConverter}}"></PathIcon>

View File

@@ -18,15 +18,16 @@ public class BreadcrumbDemoViewModel: ObservableObject
new BreadcrumbDemoItem() { Section = "Page 1", Icon = "Page" },
new BreadcrumbDemoItem() { Section = "Page 2", Icon = "Page" },
new BreadcrumbDemoItem() { Section = "Page 3", Icon = "Page" },
new BreadcrumbDemoItem() { Section = "Page 4", Icon = "Page" },
new BreadcrumbDemoItem() { Section = "Page 4", Icon = "Page", IsReadOnly = true},
};
}
}
public class BreadcrumbDemoItem: ObservableObject
public partial class BreadcrumbDemoItem: ObservableObject
{
public string Section { get; set; }
public string Icon { get; set; }
[ObservableProperty] private bool _isReadOnly;
public ICommand Command { get; set; }

View File

@@ -19,13 +19,9 @@
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
</ControlTemplate>
</Setter>
<Style Selector="^[IsReadOnly=True]">
<Setter Property="ItemContainerTheme" Value="{DynamicResource ReadOnlyBreadcrumbItem}"></Setter>
</Style>
</ControlTheme>
<ControlTheme x:Key="{x:Type u:BreadcrumbItem}" TargetType="u:BreadcrumbItem">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<ControlTemplate TargetType="u:BreadcrumbItem">
<Border Background="Transparent">
@@ -61,6 +57,9 @@
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^[IsReadOnly=False]">
<Setter Property="Cursor" Value="Hand"></Setter>
</Style>
<Style Selector="^:last">
<Style Selector="^ /template/ ContentPresenter#PART_IconPresenter">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText0}" />
@@ -72,7 +71,7 @@
<Style Selector="^ /template/ ContentPresenter#Separator">
<Setter Property="IsVisible" Value="False" />
</Style>
<Style Selector="^:pointerover">
<Style Selector="^[IsReadOnly=False]:pointerover">
<Style Selector="^ /template/ ContentPresenter#PART_IconPresenter">
<Setter Property="Foreground" Value="{DynamicResource SemiBlue5}" />
</Style>
@@ -81,7 +80,7 @@
</Style>
</Style>
</Style>
<Style Selector="^:pointerover">
<Style Selector="^[IsReadOnly=False]:pointerover">
<Style Selector="^ /template/ ContentPresenter#PART_IconPresenter">
<Setter Property="Foreground" Value="{DynamicResource SemiBlue5}" />
</Style>
@@ -90,55 +89,4 @@
</Style>
</Style>
</ControlTheme>
<ControlTheme x:Key="ReadOnlyBreadcrumbItem" TargetType="u:BreadcrumbItem">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<ControlTemplate TargetType="u:BreadcrumbItem">
<Border Background="Transparent">
<StackPanel Orientation="Horizontal">
<ContentPresenter
Name="PART_IconPresenter"
Margin="0,0,4,0"
VerticalAlignment="Center"
Content="{TemplateBinding Icon}"
ContentTemplate="{TemplateBinding IconTemplate}"
Foreground="{DynamicResource SemiColorText2}"
IsVisible="{TemplateBinding Icon,
Converter={x:Static ObjectConverters.IsNotNull}}" />
<ContentPresenter
Name="PART_ContentPresenter"
VerticalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Foreground="{DynamicResource SemiColorText2}"
IsVisible="{TemplateBinding Content,
Converter={x:Static ObjectConverters.IsNotNull}}" />
<ContentPresenter
Name="Separator"
Margin="4,0 0 0"
VerticalAlignment="Center"
Content="{TemplateBinding Separator}"
Foreground="{DynamicResource SemiColorText3}">
<ContentPresenter.IsVisible>
<TemplateBinding Converter="{x:Static ObjectConverters.IsNotNull}" Property="Separator" />
</ContentPresenter.IsVisible>
</ContentPresenter>
</StackPanel>
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:last">
<Style Selector="^ /template/ ContentPresenter#PART_IconPresenter">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText0}" />
</Style>
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText0}" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style Selector="^ /template/ ContentPresenter#Separator">
<Setter Property="IsVisible" Value="False" />
</Style>
</Style>
</ControlTheme>
</ResourceDictionary>

View File

@@ -56,15 +56,6 @@ public class Breadcrumb: ItemsControl
get => GetValue(IconTemplateProperty);
set => SetValue(IconTemplateProperty, value);
}
public static readonly StyledProperty<bool> IsReadOnlyProperty = AvaloniaProperty.Register<Breadcrumb, bool>(
nameof(IsReadOnly));
public bool IsReadOnly
{
get => GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
static Breadcrumb()
{

View File

@@ -49,11 +49,19 @@ public class BreadcrumbItem: ContentControl
set => SetValue(IconTemplateProperty, value);
}
public static readonly StyledProperty<bool> IsReadOnlyProperty = AvaloniaProperty.Register<BreadcrumbItem, bool>(
nameof(IsReadOnly));
public bool IsReadOnly
{
get => GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
var parent = this.FindLogicalAncestorOfType<Breadcrumb>();
if (parent?.IsReadOnly != true)
if (!IsReadOnly)
{
Command?.Execute(null);
}