wip: simplify container preparation.
This commit is contained in:
@@ -48,9 +48,15 @@
|
|||||||
<u:NavMenu
|
<u:NavMenu
|
||||||
Name="menu"
|
Name="menu"
|
||||||
ItemsSource="{Binding MenuItems}"
|
ItemsSource="{Binding MenuItems}"
|
||||||
HeaderBinding="{Binding}"
|
HeaderBinding="{Binding Header}"
|
||||||
SubMenuBinding="{Binding Children}"
|
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>
|
</StackPanel>
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
<ControlTheme x:Key="{x:Type u:NavMenuItem}" TargetType="u:NavMenuItem">
|
<ControlTheme x:Key="{x:Type u:NavMenuItem}" TargetType="u:NavMenuItem">
|
||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<ControlTemplate TargetType="u:NavMenuItem">
|
<ControlTemplate TargetType="u:NavMenuItem">
|
||||||
<Grid RowDefinitions="Auto, *" Background="{TemplateBinding Background}">
|
<Grid Background="{TemplateBinding Background}" RowDefinitions="Auto, *">
|
||||||
<Grid Grid.Row="0">
|
<Grid Grid.Row="0">
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
|
<ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
|
||||||
@@ -25,15 +25,16 @@
|
|||||||
<ColumnDefinition Width="Auto" SharedSizeGroup="Expander" />
|
<ColumnDefinition Width="Auto" SharedSizeGroup="Expander" />
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
<ContentPresenter
|
<ContentPresenter
|
||||||
HorizontalAlignment="Left"
|
|
||||||
Padding="8"
|
Padding="8"
|
||||||
|
HorizontalAlignment="Left"
|
||||||
Background="Transparent"
|
Background="Transparent"
|
||||||
Content="{TemplateBinding Icon}" />
|
Content="{TemplateBinding Icon}"
|
||||||
|
ContentTemplate="{TemplateBinding IconTemplate}" />
|
||||||
<ContentPresenter
|
<ContentPresenter
|
||||||
Grid.Column="1"
|
Grid.Column="1"
|
||||||
Padding="0 8"
|
Padding="0,8"
|
||||||
Background="Transparent"
|
Background="Transparent"
|
||||||
Content="{TemplateBinding Header}" />
|
Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" />
|
||||||
</Grid>
|
</Grid>
|
||||||
<ItemsPresenter
|
<ItemsPresenter
|
||||||
Grid.Row="1"
|
Grid.Row="1"
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.Primitives;
|
using Avalonia.Controls.Primitives;
|
||||||
|
using Avalonia.Controls.Templates;
|
||||||
using Avalonia.Data;
|
using Avalonia.Data;
|
||||||
using Avalonia.LogicalTree;
|
using Avalonia.LogicalTree;
|
||||||
using Avalonia.Metadata;
|
using Avalonia.Metadata;
|
||||||
@@ -63,6 +64,24 @@ public class NavMenu: ItemsControl
|
|||||||
set => SetValue(CommandBindingProperty, value);
|
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()
|
static NavMenu()
|
||||||
{
|
{
|
||||||
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
|
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
|
||||||
@@ -83,30 +102,6 @@ public class NavMenu: ItemsControl
|
|||||||
return new NavMenuItem();
|
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)
|
internal void SelectItem(NavMenuItem item, NavMenuItem parent)
|
||||||
{
|
{
|
||||||
// if (item.IsSelected) return;
|
// if (item.IsSelected) return;
|
||||||
@@ -116,12 +111,9 @@ public class NavMenu: ItemsControl
|
|||||||
{
|
{
|
||||||
continue;
|
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)
|
if (item.DataContext is not null && item.DataContext != this.DataContext)
|
||||||
|
|||||||
@@ -84,6 +84,28 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
|||||||
private set => SetAndRaise(IsHighlightedProperty, ref _isHighlighted, value);
|
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; }
|
internal int Level { get; set; }
|
||||||
|
|
||||||
@@ -110,34 +132,32 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
|||||||
return new NavMenuItem();
|
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)
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnAttachedToVisualTree(e);
|
base.OnAttachedToVisualTree(e);
|
||||||
_rootMenu = GetRootMenu();
|
_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)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
@@ -154,6 +174,10 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
|||||||
if (this.ItemCount == 0)
|
if (this.ItemCount == 0)
|
||||||
{
|
{
|
||||||
SelectItem(this);
|
SelectItem(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
Command?.Execute(CommandParameter);
|
Command?.Execute(CommandParameter);
|
||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user