From 82351d868a0ddeec3b036ed8db2aa5388305188e Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Sun, 1 Apr 2018 18:44:31 +0200 Subject: [PATCH] Fixes #542 by properly reducing min/max items per row in InRibbonGallery --- Changelog.md | 1 + Fluent.Ribbon.Showcase/TestContent.xaml | 2 +- .../Controls/GalleryGroupContainer.cs | 28 +++++++++++++++---- Fluent.Ribbon/Controls/GalleryPanel.cs | 10 +++++-- Fluent.Ribbon/Controls/InRibbonGallery.cs | 14 ++++++---- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/Changelog.md b/Changelog.md index f46514222..e0825ce6f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -8,6 +8,7 @@ - ### Bug fixes - [#535](../../issues/535) - BorderBush on bottom of RibbonTabItem (and Ribbon) + - [#542](../../issues/542) - InRibbonGallery not reducing properly - [#543](../../issues/543) - Using images that can't be found during design time crashes designer A generic "error" image is rendered during design time and an exception is thrown during runtime. - [#551](../../issues/551) - "Auto" size for ribbon group box header to support custom font sizes (thanks @chrfin) diff --git a/Fluent.Ribbon.Showcase/TestContent.xaml b/Fluent.Ribbon.Showcase/TestContent.xaml index 129a897ea..0d4c0a0c6 100644 --- a/Fluent.Ribbon.Showcase/TestContent.xaml +++ b/Fluent.Ribbon.Showcase/TestContent.xaml @@ -1883,7 +1883,7 @@ it will reduce all InRibbonGalleries in this group by one item--> + ReduceOrder="(FirstGalleryGroup),(FirstGalleryGroup),(FirstGalleryGroup),(FirstGalleryGroup),(FirstGalleryGroup),(SecondGalleryGroup),(SecondGalleryGroup),(SecondGalleryGroup),(SecondGalleryGroup),(A),(A),(A),(A),(A),(A),(A),(A),(A),(A)"> diff --git a/Fluent.Ribbon/Controls/GalleryGroupContainer.cs b/Fluent.Ribbon/Controls/GalleryGroupContainer.cs index ec0cb3c0e..ee724e5a5 100644 --- a/Fluent.Ribbon/Controls/GalleryGroupContainer.cs +++ b/Fluent.Ribbon/Controls/GalleryGroupContainer.cs @@ -129,7 +129,7 @@ public int MinItemsInRow /// public static readonly DependencyProperty MinItemsInRowProperty = DependencyProperty.Register(nameof(MinItemsInRow), typeof(int), - typeof(GalleryGroupContainer), new PropertyMetadata(IntBoxes.Zero, OnMaxMinItemsInRowChanged)); + typeof(GalleryGroupContainer), new FrameworkPropertyMetadata(IntBoxes.Zero, FrameworkPropertyMetadataOptions.AffectsMeasure, OnMaxMinItemsInRowChanged)); #endregion @@ -150,7 +150,7 @@ public int MaxItemsInRow /// public static readonly DependencyProperty MaxItemsInRowProperty = DependencyProperty.Register(nameof(MaxItemsInRow), typeof(int), - typeof(GalleryGroupContainer), new PropertyMetadata(int.MaxValue, OnMaxMinItemsInRowChanged)); + typeof(GalleryGroupContainer), new FrameworkPropertyMetadata(int.MaxValue, FrameworkPropertyMetadataOptions.AffectsMeasure, OnMaxMinItemsInRowChanged)); #endregion @@ -166,12 +166,21 @@ private static void OnMaxMinItemsInRowChanged(DependencyObject d, DependencyProp { var galleryGroupContainer = (GalleryGroupContainer)d; galleryGroupContainer.minMaxWidthNeedsToBeUpdated = true; + galleryGroupContainer.UpdateMinAndMaxWidth(); } #endregion #region Initialization + /// + /// Creates a new instance of . + /// + public GalleryGroupContainer() + { + this.Unloaded += this.HandleUnloaded; + } + /// /// Static constructor /// @@ -227,8 +236,8 @@ private void UpdateMinAndMaxWidth() if (this.Orientation == Orientation.Vertical) { // Min/Max is used for Horizontal layout only - this.RealItemsPanel.MinWidth = 0; - this.RealItemsPanel.MaxWidth = double.PositiveInfinity; + this.MinWidth = 0; + this.MaxWidth = double.PositiveInfinity; return; } @@ -239,8 +248,8 @@ private void UpdateMinAndMaxWidth() return; } - this.RealItemsPanel.MinWidth = (Math.Min(this.Items.Count, this.MinItemsInRow) * itemWidth) + 0.1; - this.RealItemsPanel.MaxWidth = (Math.Min(this.Items.Count, this.MaxItemsInRow) * itemWidth) + 0.1; + this.MinWidth = (Math.Min(this.Items.Count, this.MinItemsInRow) * itemWidth) + 0.1; + this.MaxWidth = (Math.Min(this.Items.Count, this.MaxItemsInRow) * itemWidth) + 0.1; } private void HandleLoaded(object sender, RoutedEventArgs e) @@ -255,6 +264,13 @@ private void HandleLoaded(object sender, RoutedEventArgs e) this.InvalidateMeasure(); } + private void HandleUnloaded(object sender, RoutedEventArgs e) + { + this.itemsPanel = null; + + this.minMaxWidthNeedsToBeUpdated = true; + } + /// /// Determinates item's size (return Size.Empty in case of it is not possible) /// diff --git a/Fluent.Ribbon/Controls/GalleryPanel.cs b/Fluent.Ribbon/Controls/GalleryPanel.cs index fa85738ad..1485bc9c4 100644 --- a/Fluent.Ribbon/Controls/GalleryPanel.cs +++ b/Fluent.Ribbon/Controls/GalleryPanel.cs @@ -251,7 +251,7 @@ public int MinItemsInRow /// public static readonly DependencyProperty MinItemsInRowProperty = DependencyProperty.Register(nameof(MinItemsInRow), typeof(int), - typeof(GalleryPanel), new PropertyMetadata(1)); + typeof(GalleryPanel), new PropertyMetadata(1, OnMinOrMaxItemsInRowChanged)); #endregion @@ -272,7 +272,13 @@ public int MaxItemsInRow /// public static readonly DependencyProperty MaxItemsInRowProperty = DependencyProperty.Register(nameof(MaxItemsInRow), typeof(int), - typeof(GalleryPanel), new PropertyMetadata(int.MaxValue)); + typeof(GalleryPanel), new PropertyMetadata(int.MaxValue, OnMinOrMaxItemsInRowChanged)); + + private static void OnMinOrMaxItemsInRowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var galleryPanel = (GalleryPanel)d; + galleryPanel.UpdateMinAndMaxWidth(); + } #endregion diff --git a/Fluent.Ribbon/Controls/InRibbonGallery.cs b/Fluent.Ribbon/Controls/InRibbonGallery.cs index 40b2fe5c7..0c4c43f0d 100644 --- a/Fluent.Ribbon/Controls/InRibbonGallery.cs +++ b/Fluent.Ribbon/Controls/InRibbonGallery.cs @@ -1581,10 +1581,11 @@ public void Enlarge() { this.IsCollapsed = false; } - else if (this.galleryPanel.MinItemsInRow < this.MaxItemsInRow) + else if (this.galleryPanel.MinItemsInRow < this.MinItemsInRow + || this.galleryPanel.MaxItemsInRow < this.MaxItemsInRow) { - this.galleryPanel.MinItemsInRow++; - this.galleryPanel.MaxItemsInRow = this.galleryPanel.MinItemsInRow; + this.galleryPanel.MinItemsInRow = Math.Min(this.galleryPanel.MinItemsInRow + 1, this.MinItemsInRow); + this.galleryPanel.MaxItemsInRow = Math.Min(this.galleryPanel.MaxItemsInRow + 1, this.MaxItemsInRow); } else { @@ -1601,10 +1602,11 @@ public void Enlarge() /// public void Reduce() { - if (this.galleryPanel.MinItemsInRow > this.MinItemsInRow) + if (this.galleryPanel.MinItemsInRow > 1 + || this.galleryPanel.MaxItemsInRow > 1) { - this.galleryPanel.MinItemsInRow--; - this.galleryPanel.MaxItemsInRow = this.galleryPanel.MinItemsInRow; + this.galleryPanel.MinItemsInRow = Math.Max(this.galleryPanel.MinItemsInRow - 1, 0); + this.galleryPanel.MaxItemsInRow = Math.Max(this.galleryPanel.MaxItemsInRow - 1, 0); } else if (this.CanCollapseToButton && this.IsCollapsed == false)