From b1595e39069b53607c1eb38f22d9688083bcab7b Mon Sep 17 00:00:00 2001 From: Jan Karger Date: Wed, 18 Oct 2017 15:38:24 +0200 Subject: [PATCH 1/2] New ScrollViewerHelper class with new attached property IsHorizontalScrollWheelEnabled. If it's set to true and a horizontal ScrollBar is visible then the mouse wheel scrolls to left and right. move VerticalScrollBarOnLeftSide attached property to ScrollViewerHelper --- docs/release-notes/1.6.0.md | 3 + .../Controls/Helper/ScrollBarHelper.cs | 27 +++-- .../Controls/Helper/ScrollViewerHelper.cs | 98 +++++++++++++++++++ .../MahApps.Metro.Shared.projitems | 1 + .../Controls.AnimatedSingleRowTabControl.xaml | 2 + .../Styles/Controls.Scrollbars.xaml | 4 +- .../MahApps.Metro/Themes/HamburgerMenu.xaml | 4 +- .../MetroAnimatedSingleRowTabControl.xaml | 2 + 8 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollViewerHelper.cs diff --git a/docs/release-notes/1.6.0.md b/docs/release-notes/1.6.0.md index 5c6b98200b..f8c12cf044 100644 --- a/docs/release-notes/1.6.0.md +++ b/docs/release-notes/1.6.0.md @@ -65,6 +65,9 @@ ``` +- New `ScrollViewerHelper` class + + New attached property `IsHorizontalScrollWheelEnabled`. If it's set to true and a horizontal ScrollBar is visible then the mouse wheel scrolls to left and right. + + `VerticalScrollBarOnLeftSide` attached property moved from ScrollBarHelper to ScrollViewerHelper. ScrollBarHelper is now marked as obsolete. ## Breaking Change diff --git a/src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollBarHelper.cs b/src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollBarHelper.cs index e753bd07e4..97c31d9cf0 100644 --- a/src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollBarHelper.cs +++ b/src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollBarHelper.cs @@ -1,19 +1,33 @@ -using System.Windows; +using System; +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; namespace MahApps.Metro.Controls { - using System.ComponentModel; - using System.Windows.Controls; - + [Obsolete("This helper class will be deleted in the next release. Instead use the ScrollViewerHelper.")] public static class ScrollBarHelper { /// /// This property can be used to set vertical scrollbar left side from the tabpanel (look at MetroAnimatedSingleRowTabControl) /// + [Obsolete("This attached property will be deleted in the next release. Instead use ScrollViewerHelper.VerticalScrollBarOnLeftSide attached property.")] public static readonly DependencyProperty VerticalScrollBarOnLeftSideProperty = - DependencyProperty.RegisterAttached("VerticalScrollBarOnLeftSide", typeof(bool), typeof(ScrollBarHelper), - new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.Inherits)); + DependencyProperty.RegisterAttached("VerticalScrollBarOnLeftSide", + typeof(bool), + typeof(ScrollBarHelper), + new FrameworkPropertyMetadata(false, + FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.Inherits, + (o, e) => + { + var element = o as ScrollViewer; + if (element != null && e.OldValue != e.NewValue && e.NewValue is bool) + { + ScrollViewerHelper.SetVerticalScrollBarOnLeftSide(element, (bool)e.NewValue); + } + })); + [Obsolete("This attached property will be deleted in the next release. Instead use ScrollViewerHelper.VerticalScrollBarOnLeftSide attached property.")] [Category(AppName.MahApps)] [AttachedPropertyBrowsableForType(typeof(ScrollViewer))] public static bool GetVerticalScrollBarOnLeftSide(ScrollViewer obj) @@ -21,6 +35,7 @@ public static bool GetVerticalScrollBarOnLeftSide(ScrollViewer obj) return (bool)obj.GetValue(VerticalScrollBarOnLeftSideProperty); } + [Obsolete("This attached property will be deleted in the next release. Instead use ScrollViewerHelper.VerticalScrollBarOnLeftSide attached property.")] public static void SetVerticalScrollBarOnLeftSide(ScrollViewer obj, bool value) { obj.SetValue(VerticalScrollBarOnLeftSideProperty, value); diff --git a/src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollViewerHelper.cs b/src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollViewerHelper.cs new file mode 100644 index 0000000000..b930befc4e --- /dev/null +++ b/src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollViewerHelper.cs @@ -0,0 +1,98 @@ +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; + +namespace MahApps.Metro.Controls +{ + public static class ScrollViewerHelper + { + /// + /// Identifies the VerticalScrollBarOnLeftSide attached property. + /// This property can be used to set vertical scrollbar left side from the tabpanel (look at MetroAnimatedSingleRowTabControl) + /// + public static readonly DependencyProperty VerticalScrollBarOnLeftSideProperty = + DependencyProperty.RegisterAttached("VerticalScrollBarOnLeftSide", + typeof(bool), + typeof(ScrollViewerHelper), + new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.Inherits)); + + /// + /// Gets whether the vertical ScrollBar is on the left side or not. + /// + [Category(AppName.MahApps)] + [AttachedPropertyBrowsableForType(typeof(ScrollViewer))] + public static bool GetVerticalScrollBarOnLeftSide(UIElement element) + { + return (bool)element.GetValue(VerticalScrollBarOnLeftSideProperty); + } + + /// + /// Sets whether the vertical ScrollBar should be on the left side or not. + /// + [Category(AppName.MahApps)] + [AttachedPropertyBrowsableForType(typeof(ScrollViewer))] + public static void SetVerticalScrollBarOnLeftSide(UIElement element, bool value) + { + element.SetValue(VerticalScrollBarOnLeftSideProperty, value); + } + + /// + /// Identifies the IsHorizontalScrollWheelEnabled attached property. + /// + public static readonly DependencyProperty IsHorizontalScrollWheelEnabledProperty = + DependencyProperty.RegisterAttached("IsHorizontalScrollWheelEnabled", + typeof(bool), + typeof(ScrollViewerHelper), + new PropertyMetadata(false, OnIsHorizontalScrollWheelEnabledPropertyChangedCallback)); + + private static void OnIsHorizontalScrollWheelEnabledPropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e) + { + var scrollViewer = o as ScrollViewer; + if (scrollViewer != null && e.NewValue != e.OldValue && e.NewValue is bool) + { + scrollViewer.PreviewMouseWheel -= ScrollViewerOnPreviewMouseWheel; + if ((bool)e.NewValue) + { + scrollViewer.PreviewMouseWheel += ScrollViewerOnPreviewMouseWheel; + } + } + } + + private static void ScrollViewerOnPreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) + { + var scrollViewer = sender as ScrollViewer; + if (scrollViewer != null && scrollViewer.HorizontalScrollBarVisibility != ScrollBarVisibility.Disabled) + { + if (e.Delta > 0) + { + scrollViewer.LineLeft(); + } + else + { + scrollViewer.LineRight(); + } + e.Handled = true; + } + } + + /// + /// Gets whether the ScrollViewer is scrolling horizontal by using the mouse wheel. + /// + [Category(AppName.MahApps)] + [AttachedPropertyBrowsableForType(typeof(UIElement))] + public static bool GetIsHorizontalScrollWheelEnabled(UIElement element) + { + return (bool)element.GetValue(IsHorizontalScrollWheelEnabledProperty); + } + + /// + /// Sets whether the ScrollViewer should be scroll horizontal by using the mouse wheel. + /// + [Category(AppName.MahApps)] + [AttachedPropertyBrowsableForType(typeof(UIElement))] + public static void SetIsHorizontalScrollWheelEnabled(UIElement element, bool value) + { + element.SetValue(IsHorizontalScrollWheelEnabledProperty, value); + } + } +} diff --git a/src/MahApps.Metro/MahApps.Metro.Shared/MahApps.Metro.Shared.projitems b/src/MahApps.Metro/MahApps.Metro.Shared/MahApps.Metro.Shared.projitems index 82514e8038..2a61f77c72 100644 --- a/src/MahApps.Metro/MahApps.Metro.Shared/MahApps.Metro.Shared.projitems +++ b/src/MahApps.Metro/MahApps.Metro.Shared/MahApps.Metro.Shared.projitems @@ -78,6 +78,7 @@ + diff --git a/src/MahApps.Metro/MahApps.Metro/Styles/Controls.AnimatedSingleRowTabControl.xaml b/src/MahApps.Metro/MahApps.Metro/Styles/Controls.AnimatedSingleRowTabControl.xaml index 1df290d09c..ec31b02e6a 100644 --- a/src/MahApps.Metro/MahApps.Metro/Styles/Controls.AnimatedSingleRowTabControl.xaml +++ b/src/MahApps.Metro/MahApps.Metro/Styles/Controls.AnimatedSingleRowTabControl.xaml @@ -196,6 +196,7 @@ @@ -311,6 +312,7 @@