A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

77 lines
2.6 KiB

using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
namespace ControlSamples
{
public class HamburgerMenu : TabControl
{
private SplitView? _splitView;
public static readonly StyledProperty<IBrush?> PaneBackgroundProperty =
SplitView.PaneBackgroundProperty.AddOwner<HamburgerMenu>();
public IBrush? PaneBackground
{
get => GetValue(PaneBackgroundProperty);
set => SetValue(PaneBackgroundProperty, value);
}
public static readonly StyledProperty<IBrush?> ContentBackgroundProperty =
AvaloniaProperty.Register<HamburgerMenu, IBrush?>(nameof(ContentBackground));
public IBrush? ContentBackground
{
get => GetValue(ContentBackgroundProperty);
set => SetValue(ContentBackgroundProperty, value);
}
public static readonly StyledProperty<int> ExpandedModeThresholdWidthProperty =
AvaloniaProperty.Register<HamburgerMenu, int>(nameof(ExpandedModeThresholdWidth), 1008);
public int ExpandedModeThresholdWidth
{
get => GetValue(ExpandedModeThresholdWidthProperty);
set => SetValue(ExpandedModeThresholdWidthProperty, value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_splitView = e.NameScope.Find<SplitView>("PART_NavigationPane");
}
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);
if (change.Property == BoundsProperty && _splitView is not null)
{
var oldBounds = change.OldValue.GetValueOrDefault<Rect>();
var newBounds = change.NewValue.GetValueOrDefault<Rect>();
EnsureSplitViewMode(oldBounds, newBounds);
}
}
private void EnsureSplitViewMode(Rect oldBounds, Rect newBounds)
{
if (_splitView is not null)
{
var threshold = ExpandedModeThresholdWidth;
if (newBounds.Width >= threshold && oldBounds.Width < threshold)
{
_splitView.DisplayMode = SplitViewDisplayMode.Inline;
_splitView.IsPaneOpen = true;
}
else if (newBounds.Width < threshold && oldBounds.Width >= threshold)
{
_splitView.DisplayMode = SplitViewDisplayMode.Overlay;
_splitView.IsPaneOpen = false;
}
}
}
}
}