From 8fbcbc3851f6cf4bb9d5a8502b95584eaffafbe2 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 9 May 2020 21:13:09 +0200 Subject: [PATCH] Fix TemplateBinding for MinimumOpenPaneLenght and MaximumOpenPaneLength didn't work. --- .../HamburgerMenu/HamburgerMenu.Properties.cs | 60 +++++++++++++++++-- src/MahApps.Metro/Themes/HamburgerMenu.xaml | 6 +- .../Themes/HamburgerMenuTemplate.xaml | 5 +- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/MahApps.Metro/Controls/HamburgerMenu/HamburgerMenu.Properties.cs b/src/MahApps.Metro/Controls/HamburgerMenu/HamburgerMenu.Properties.cs index 50001ba315..20d1080f00 100644 --- a/src/MahApps.Metro/Controls/HamburgerMenu/HamburgerMenu.Properties.cs +++ b/src/MahApps.Metro/Controls/HamburgerMenu/HamburgerMenu.Properties.cs @@ -21,14 +21,66 @@ public partial class HamburgerMenu private static void OpenPaneLengthPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { - if (args.NewValue != args.OldValue) + if (args.NewValue != args.OldValue && dependencyObject is HamburgerMenu hamburgerMenu && hamburgerMenu.ValidateOpenPaneLenth()) { - (dependencyObject as HamburgerMenu)?.ChangeItemFocusVisualStyle(); + hamburgerMenu.ChangeItemFocusVisualStyle(); + } + } + + private bool ValidateOpenPaneLenth() + { + if (this.ActualWidth > 0) + { + double minWidth = 0; + + // Get the minimum needed width + if (this.DisplayMode == SplitViewDisplayMode.CompactInline || this.DisplayMode == SplitViewDisplayMode.CompactOverlay) + { + minWidth = Math.Max(this.CompactPaneLength, this.MinimumOpenPaneLength); + } + else + { + minWidth = Math.Max(0, this.MinimumOpenPaneLength); + } + + if (minWidth < 0) + { + minWidth = 0; + } + + // Get the maximum allowed width + double maxWidth = Math.Min(this.ActualWidth, this.MaximumOpenPaneLength); + + // Check if max < min + if (maxWidth < minWidth) + { + minWidth = maxWidth; + } + + // Check is OpenPaneLength is valid + if (OpenPaneLength < minWidth) + { + SetCurrentValue(OpenPaneLengthProperty, minWidth); + return false; + } + else if (OpenPaneLength > maxWidth) + { + SetCurrentValue(OpenPaneLengthProperty, maxWidth); + return false; + } + else + { + return true; + } + } + else + { + return false; } } /// Identifies the dependency property. - public static readonly DependencyProperty MinimumOpenPaneLengthProperty = DependencyProperty.Register(nameof(MinimumOpenPaneLength), typeof(double), typeof(HamburgerMenu), new PropertyMetadata(100d)); + public static readonly DependencyProperty MinimumOpenPaneLengthProperty = DependencyProperty.Register(nameof(MinimumOpenPaneLength), typeof(double), typeof(HamburgerMenu), new PropertyMetadata(100d, OpenPaneLengthPropertyChangedCallback)); /// /// Gets or sets the minimum width of the pane when it's fully expanded. @@ -45,7 +97,7 @@ public double MinimumOpenPaneLength /// Identifies the dependency property. - public static readonly DependencyProperty MaximumOpenPaneLengthProperty = DependencyProperty.Register(nameof(MaximumOpenPaneLength), typeof(double), typeof(HamburgerMenu), new PropertyMetadata(500d)); + public static readonly DependencyProperty MaximumOpenPaneLengthProperty = DependencyProperty.Register(nameof(MaximumOpenPaneLength), typeof(double), typeof(HamburgerMenu), new PropertyMetadata(500d, OpenPaneLengthPropertyChangedCallback)); /// /// Gets or sets the maximum width of the pane when it's fully expanded. diff --git a/src/MahApps.Metro/Themes/HamburgerMenu.xaml b/src/MahApps.Metro/Themes/HamburgerMenu.xaml index d0d6e417f1..bc25026ca1 100644 --- a/src/MahApps.Metro/Themes/HamburgerMenu.xaml +++ b/src/MahApps.Metro/Themes/HamburgerMenu.xaml @@ -4,14 +4,16 @@ -