feat: deal with selection.

This commit is contained in:
rabbitism
2024-02-12 00:37:20 +08:00
parent 5e5e0844e1
commit 0a3dcf0d8c
4 changed files with 47 additions and 16 deletions

View File

@@ -5,7 +5,7 @@
xmlns:u="https://irihi.tech/ursa" xmlns:u="https://irihi.tech/ursa"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Pages.NavMenuDemo"> x:Class="Ursa.Demo.Pages.NavMenuDemo">
<u:NavMenu> <u:NavMenu Name="menu">
<u:NavMenuItem Header="Menu 1"> <u:NavMenuItem Header="Menu 1">
<u:NavMenuItem.Icon> <u:NavMenuItem.Icon>
<Rectangle Width="10" Height="10" Fill="Red"></Rectangle> <Rectangle Width="10" Height="10" Fill="Red"></Rectangle>
@@ -21,5 +21,7 @@
<Rectangle Width="30" Height="10" Fill="Red"></Rectangle> <Rectangle Width="30" Height="10" Fill="Red"></Rectangle>
</u:NavMenuItem.Icon> </u:NavMenuItem.Icon>
</u:NavMenuItem> </u:NavMenuItem>
<u:Divider Content="Divider"></u:Divider>
<TextBlock Text="{Binding #menu.SelectedItem}"></TextBlock>
</u:NavMenu> </u:NavMenu>
</UserControl> </UserControl>

View File

@@ -14,19 +14,17 @@
<ControlTheme TargetType="u:NavMenuItem" x:Key="{x:Type u:NavMenuItem}"> <ControlTheme TargetType="u:NavMenuItem" x:Key="{x:Type u:NavMenuItem}">
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate TargetType="u:NavMenuItem"> <ControlTemplate TargetType="u:NavMenuItem">
<Expander> <Grid RowDefinitions="Auto, *">
<Expander.Header> <Grid Grid.Row="0">
<Grid> <Grid.ColumnDefinitions>
<Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Icon" /> <ColumnDefinition Width="*" />
<ColumnDefinition Width="*" /> </Grid.ColumnDefinitions>
</Grid.ColumnDefinitions> <ContentPresenter Content="{TemplateBinding Icon}" HorizontalAlignment="Left" />
<ContentPresenter Content="{TemplateBinding Icon}" HorizontalAlignment="Left" /> <ContentPresenter Grid.Column="1" Content="{TemplateBinding Header}" />
<ContentPresenter Grid.Column="1" Content="{TemplateBinding Header}" /> </Grid>
</Grid> <ItemsPresenter Grid.Row="1" ItemsPanel="{TemplateBinding ItemsPanel}" />
</Expander.Header> </Grid>
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
</Expander>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
</ControlTheme> </ControlTheme>

View File

@@ -1,11 +1,33 @@
using Avalonia.Controls; using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.LogicalTree; using Avalonia.LogicalTree;
namespace Ursa.Controls; namespace Ursa.Controls;
public class NavMenu: SelectingItemsControl public class NavMenu: ItemsControl
{ {
public static readonly StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<NavMenu, object?>(
nameof(SelectedItem), defaultBindingMode: BindingMode.TwoWay);
public object? SelectedItem
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
static NavMenu()
{
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
}
private void OnSelectedItemChange(AvaloniaPropertyChangedEventArgs<object?> args)
{
Debug.WriteLine(args.NewValue.Value);
}
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
{ {
return NeedsContainer<NavMenuItem>(item, out recycleKey); return NeedsContainer<NavMenuItem>(item, out recycleKey);
@@ -27,6 +49,14 @@ public class NavMenu: SelectingItemsControl
child.IsSelected = false; child.IsSelected = false;
} }
} }
if (item.DataContext is not null && item.DataContext != this.DataContext)
{
SelectedItem = item.DataContext;
}
else
{
SelectedItem = item;
}
item.IsSelected = true; item.IsSelected = true;
} }
} }

View File

@@ -60,6 +60,7 @@ public class NavMenuItem: HeaderedSelectingItemsControl
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
_rootMenu = GetRootMenu(); _rootMenu = GetRootMenu();
UpdateSelection(1);
} }
protected override void OnPointerPressed(PointerPressedEventArgs e) protected override void OnPointerPressed(PointerPressedEventArgs e)