wip: simplify container preparation.
This commit is contained in:
@@ -48,9 +48,15 @@
|
||||
<u:NavMenu
|
||||
Name="menu"
|
||||
ItemsSource="{Binding MenuItems}"
|
||||
HeaderBinding="{Binding}"
|
||||
HeaderBinding="{Binding Header}"
|
||||
SubMenuBinding="{Binding Children}"
|
||||
IconBinding="{Binding Header}"/>
|
||||
IconBinding="{Binding}">
|
||||
<u:NavMenu.IconTemplate>
|
||||
<DataTemplate>
|
||||
<Rectangle Width="10" Height="10" Fill="Blue"></Rectangle>
|
||||
</DataTemplate>
|
||||
</u:NavMenu.IconTemplate>
|
||||
</u:NavMenu>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</UserControl>
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
<ControlTheme x:Key="{x:Type u:NavMenuItem}" TargetType="u:NavMenuItem">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:NavMenuItem">
|
||||
<Grid RowDefinitions="Auto, *" Background="{TemplateBinding Background}">
|
||||
<Grid Background="{TemplateBinding Background}" RowDefinitions="Auto, *">
|
||||
<Grid Grid.Row="0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
|
||||
@@ -25,15 +25,16 @@
|
||||
<ColumnDefinition Width="Auto" SharedSizeGroup="Expander" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Left"
|
||||
Padding="8"
|
||||
HorizontalAlignment="Left"
|
||||
Background="Transparent"
|
||||
Content="{TemplateBinding Icon}" />
|
||||
Content="{TemplateBinding Icon}"
|
||||
ContentTemplate="{TemplateBinding IconTemplate}" />
|
||||
<ContentPresenter
|
||||
Grid.Column="1"
|
||||
Padding="0 8"
|
||||
Padding="0,8"
|
||||
Background="Transparent"
|
||||
Content="{TemplateBinding Header}" />
|
||||
Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" />
|
||||
</Grid>
|
||||
<ItemsPresenter
|
||||
Grid.Row="1"
|
||||
@@ -43,7 +44,7 @@
|
||||
</Grid>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
|
||||
|
||||
<Style Selector="^:selected">
|
||||
<Setter Property="Background" Value="{DynamicResource NavigationMenuItemSelectedBackground}" />
|
||||
</Style>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.LogicalTree;
|
||||
using Avalonia.Metadata;
|
||||
@@ -63,6 +64,24 @@ public class NavMenu: ItemsControl
|
||||
set => SetValue(CommandBindingProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate?> HeaderTemplateProperty = AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
|
||||
nameof(HeaderTemplate));
|
||||
|
||||
public IDataTemplate? HeaderTemplate
|
||||
{
|
||||
get => GetValue(HeaderTemplateProperty);
|
||||
set => SetValue(HeaderTemplateProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty = AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
|
||||
nameof(IconTemplate));
|
||||
|
||||
public IDataTemplate? IconTemplate
|
||||
{
|
||||
get => GetValue(IconTemplateProperty);
|
||||
set => SetValue(IconTemplateProperty, value);
|
||||
}
|
||||
|
||||
static NavMenu()
|
||||
{
|
||||
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
|
||||
@@ -83,30 +102,6 @@ public class NavMenu: ItemsControl
|
||||
return new NavMenuItem();
|
||||
}
|
||||
|
||||
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
|
||||
{
|
||||
base.PrepareContainerForItemOverride(container, item, index);
|
||||
if (container is NavMenuItem navMenuItem)
|
||||
{
|
||||
if (IconBinding is not null)
|
||||
{
|
||||
navMenuItem[!NavMenuItem.IconProperty] = IconBinding;
|
||||
}
|
||||
if (HeaderBinding is not null)
|
||||
{
|
||||
navMenuItem[!HeaderedItemsControl.HeaderProperty] = HeaderBinding;
|
||||
}
|
||||
if (SubMenuBinding is not null)
|
||||
{
|
||||
navMenuItem[!ItemsSourceProperty] = SubMenuBinding;
|
||||
}
|
||||
if (CommandBinding is not null)
|
||||
{
|
||||
navMenuItem[!NavMenuItem.CommandProperty] = CommandBinding;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void SelectItem(NavMenuItem item, NavMenuItem parent)
|
||||
{
|
||||
// if (item.IsSelected) return;
|
||||
@@ -116,12 +111,9 @@ public class NavMenu: ItemsControl
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
if (child is NavMenuItem navMenuItem)
|
||||
{
|
||||
if (child is NavMenuItem navMenuItem)
|
||||
{
|
||||
navMenuItem.ClearSelection();
|
||||
}
|
||||
navMenuItem.ClearSelection();
|
||||
}
|
||||
}
|
||||
if (item.DataContext is not null && item.DataContext != this.DataContext)
|
||||
|
||||
@@ -83,6 +83,28 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
||||
get => _isHighlighted;
|
||||
private set => SetAndRaise(IsHighlightedProperty, ref _isHighlighted, value);
|
||||
}
|
||||
|
||||
private bool _isCollapsed;
|
||||
|
||||
public static readonly DirectProperty<NavMenuItem, bool> IsCollapsedProperty = AvaloniaProperty.RegisterDirect<NavMenuItem, bool>(
|
||||
nameof(IsCollapsed), o => o.IsCollapsed, (o, v) => o.IsCollapsed = v);
|
||||
|
||||
public bool IsCollapsed
|
||||
{
|
||||
get => _isCollapsed;
|
||||
set => SetAndRaise(IsCollapsedProperty, ref _isCollapsed, value);
|
||||
}
|
||||
|
||||
private bool _isClosed;
|
||||
|
||||
public static readonly DirectProperty<NavMenuItem, bool> IsClosedProperty = AvaloniaProperty.RegisterDirect<NavMenuItem, bool>(
|
||||
nameof(IsClosed), o => o.IsClosed, (o, v) => o.IsClosed = v);
|
||||
|
||||
public bool IsClosed
|
||||
{
|
||||
get => _isClosed;
|
||||
set => SetAndRaise(IsClosedProperty, ref _isClosed, value);
|
||||
}
|
||||
|
||||
|
||||
internal int Level { get; set; }
|
||||
@@ -109,35 +131,33 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
||||
{
|
||||
return new NavMenuItem();
|
||||
}
|
||||
|
||||
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
|
||||
{
|
||||
base.PrepareContainerForItemOverride(container, item, index);
|
||||
if (container is NavMenuItem navMenuItem)
|
||||
{
|
||||
if (_rootMenu?.HeaderBinding is not null)
|
||||
{
|
||||
container[!HeaderProperty] = _rootMenu.HeaderBinding;
|
||||
}
|
||||
if (_rootMenu?.IconBinding is not null)
|
||||
{
|
||||
container[!IconProperty] = _rootMenu.IconBinding;
|
||||
}
|
||||
if (_rootMenu?.SubMenuBinding is not null)
|
||||
{
|
||||
container[!ItemsSourceProperty] = _rootMenu.SubMenuBinding;
|
||||
}
|
||||
if (_rootMenu?.CommandBinding is not null)
|
||||
{
|
||||
container[!CommandProperty] = _rootMenu.CommandBinding;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
_rootMenu = GetRootMenu();
|
||||
if (_rootMenu is not null)
|
||||
{
|
||||
if (_rootMenu.IconBinding is not null)
|
||||
{
|
||||
this[!IconProperty] = _rootMenu.IconBinding;
|
||||
}
|
||||
if (_rootMenu.HeaderBinding is not null)
|
||||
{
|
||||
this[!HeaderProperty] = _rootMenu.HeaderBinding;
|
||||
}
|
||||
if (_rootMenu.SubMenuBinding is not null)
|
||||
{
|
||||
this[!ItemsSourceProperty] = _rootMenu.SubMenuBinding;
|
||||
}
|
||||
if (_rootMenu.CommandBinding is not null)
|
||||
{
|
||||
this[!CommandProperty] = _rootMenu.CommandBinding;
|
||||
}
|
||||
this[!IconTemplateProperty] = _rootMenu[!NavMenu.IconTemplateProperty];
|
||||
this[!HeaderTemplateProperty] = _rootMenu[!NavMenu.HeaderTemplateProperty];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
@@ -154,6 +174,10 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
||||
if (this.ItemCount == 0)
|
||||
{
|
||||
SelectItem(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
Command?.Execute(CommandParameter);
|
||||
e.Handled = true;
|
||||
|
||||
Reference in New Issue
Block a user