Skip to content

Commit 23c267e

Browse files
authored
New ScrollViewerHelper class with IsHorizontalScrollWheelEnabled (#3087)
* 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 * appveyor webhook test
1 parent 6438e1f commit 23c267e

File tree

8 files changed

+131
-10
lines changed

8 files changed

+131
-10
lines changed

docs/release-notes/1.6.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<TextBox Text="Enabled" />
6666
</Controls:MetroHeader>
6767
```
68+
- New `ScrollViewerHelper` class
69+
+ 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.
70+
+ Moved `VerticalScrollBarOnLeftSide` attached property from ScrollBarHelper to ScrollViewerHelper. ScrollBarHelper is now marked as obsolete.
6871

6972
## Breaking Change
7073

src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/ScrollBarHelper.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
1-
using System.Windows;
1+
using System;
2+
using System.ComponentModel;
3+
using System.Windows;
4+
using System.Windows.Controls;
25

36
namespace MahApps.Metro.Controls
47
{
5-
using System.ComponentModel;
6-
using System.Windows.Controls;
7-
8+
[Obsolete("This helper class will be deleted in the next release. Instead use the ScrollViewerHelper.")]
89
public static class ScrollBarHelper
910
{
1011
/// <summary>
1112
/// This property can be used to set vertical scrollbar left side from the tabpanel (look at MetroAnimatedSingleRowTabControl)
1213
/// </summary>
14+
[Obsolete("This attached property will be deleted in the next release. Instead use ScrollViewerHelper.VerticalScrollBarOnLeftSide attached property.")]
1315
public static readonly DependencyProperty VerticalScrollBarOnLeftSideProperty =
14-
DependencyProperty.RegisterAttached("VerticalScrollBarOnLeftSide", typeof(bool), typeof(ScrollBarHelper),
15-
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.Inherits));
16+
DependencyProperty.RegisterAttached("VerticalScrollBarOnLeftSide",
17+
typeof(bool),
18+
typeof(ScrollBarHelper),
19+
new FrameworkPropertyMetadata(false,
20+
FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.Inherits,
21+
(o, e) =>
22+
{
23+
var element = o as ScrollViewer;
24+
if (element != null && e.OldValue != e.NewValue && e.NewValue is bool)
25+
{
26+
ScrollViewerHelper.SetVerticalScrollBarOnLeftSide(element, (bool)e.NewValue);
27+
}
28+
}));
1629

30+
[Obsolete("This attached property will be deleted in the next release. Instead use ScrollViewerHelper.VerticalScrollBarOnLeftSide attached property.")]
1731
[Category(AppName.MahApps)]
1832
[AttachedPropertyBrowsableForType(typeof(ScrollViewer))]
1933
public static bool GetVerticalScrollBarOnLeftSide(ScrollViewer obj)
2034
{
2135
return (bool)obj.GetValue(VerticalScrollBarOnLeftSideProperty);
2236
}
2337

38+
[Obsolete("This attached property will be deleted in the next release. Instead use ScrollViewerHelper.VerticalScrollBarOnLeftSide attached property.")]
2439
public static void SetVerticalScrollBarOnLeftSide(ScrollViewer obj, bool value)
2540
{
2641
obj.SetValue(VerticalScrollBarOnLeftSideProperty, value);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.ComponentModel;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
5+
namespace MahApps.Metro.Controls
6+
{
7+
public static class ScrollViewerHelper
8+
{
9+
/// <summary>
10+
/// Identifies the VerticalScrollBarOnLeftSide attached property.
11+
/// This property can be used to set vertical scrollbar left side from the tabpanel (look at MetroAnimatedSingleRowTabControl)
12+
/// </summary>
13+
public static readonly DependencyProperty VerticalScrollBarOnLeftSideProperty =
14+
DependencyProperty.RegisterAttached("VerticalScrollBarOnLeftSide",
15+
typeof(bool),
16+
typeof(ScrollViewerHelper),
17+
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.Inherits));
18+
19+
/// <summary>
20+
/// Gets whether the vertical ScrollBar is on the left side or not.
21+
/// </summary>
22+
[Category(AppName.MahApps)]
23+
[AttachedPropertyBrowsableForType(typeof(ScrollViewer))]
24+
public static bool GetVerticalScrollBarOnLeftSide(UIElement element)
25+
{
26+
return (bool)element.GetValue(VerticalScrollBarOnLeftSideProperty);
27+
}
28+
29+
/// <summary>
30+
/// Sets whether the vertical ScrollBar should be on the left side or not.
31+
/// </summary>
32+
[Category(AppName.MahApps)]
33+
[AttachedPropertyBrowsableForType(typeof(ScrollViewer))]
34+
public static void SetVerticalScrollBarOnLeftSide(UIElement element, bool value)
35+
{
36+
element.SetValue(VerticalScrollBarOnLeftSideProperty, value);
37+
}
38+
39+
/// <summary>
40+
/// Identifies the IsHorizontalScrollWheelEnabled attached property.
41+
/// </summary>
42+
public static readonly DependencyProperty IsHorizontalScrollWheelEnabledProperty =
43+
DependencyProperty.RegisterAttached("IsHorizontalScrollWheelEnabled",
44+
typeof(bool),
45+
typeof(ScrollViewerHelper),
46+
new PropertyMetadata(false, OnIsHorizontalScrollWheelEnabledPropertyChangedCallback));
47+
48+
private static void OnIsHorizontalScrollWheelEnabledPropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
49+
{
50+
var scrollViewer = o as ScrollViewer;
51+
if (scrollViewer != null && e.NewValue != e.OldValue && e.NewValue is bool)
52+
{
53+
scrollViewer.PreviewMouseWheel -= ScrollViewerOnPreviewMouseWheel;
54+
if ((bool)e.NewValue)
55+
{
56+
scrollViewer.PreviewMouseWheel += ScrollViewerOnPreviewMouseWheel;
57+
}
58+
}
59+
}
60+
61+
private static void ScrollViewerOnPreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
62+
{
63+
var scrollViewer = sender as ScrollViewer;
64+
if (scrollViewer != null && scrollViewer.HorizontalScrollBarVisibility != ScrollBarVisibility.Disabled)
65+
{
66+
if (e.Delta > 0)
67+
{
68+
scrollViewer.LineLeft();
69+
}
70+
else
71+
{
72+
scrollViewer.LineRight();
73+
}
74+
e.Handled = true;
75+
}
76+
}
77+
78+
/// <summary>
79+
/// Gets whether the ScrollViewer is scrolling horizontal by using the mouse wheel.
80+
/// </summary>
81+
[Category(AppName.MahApps)]
82+
[AttachedPropertyBrowsableForType(typeof(UIElement))]
83+
public static bool GetIsHorizontalScrollWheelEnabled(UIElement element)
84+
{
85+
return (bool)element.GetValue(IsHorizontalScrollWheelEnabledProperty);
86+
}
87+
88+
/// <summary>
89+
/// Sets whether the ScrollViewer should be scroll horizontal by using the mouse wheel.
90+
/// </summary>
91+
[Category(AppName.MahApps)]
92+
[AttachedPropertyBrowsableForType(typeof(UIElement))]
93+
public static void SetIsHorizontalScrollWheelEnabled(UIElement element, bool value)
94+
{
95+
element.SetValue(IsHorizontalScrollWheelEnabledProperty, value);
96+
}
97+
}
98+
}

src/MahApps.Metro/MahApps.Metro.Shared/MahApps.Metro.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\MouseWheelState.cs" />
7979
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\PasswordBoxHelper.cs" />
8080
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\ScrollBarHelper.cs" />
81+
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\ScrollViewerHelper.cs" />
8182
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\SliderHelper.cs" />
8283
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\TabControlHelper.cs" />
8384
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\TextBoxHelper.cs" />

src/MahApps.Metro/MahApps.Metro/Styles/Controls.AnimatedSingleRowTabControl.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
<ScrollViewer x:Name="HeaderPanelScroll"
197197
Grid.Row="0"
198198
Panel.ZIndex="1"
199+
Controls:ScrollViewerHelper.IsHorizontalScrollWheelEnabled="{TemplateBinding Controls:ScrollViewerHelper.IsHorizontalScrollWheelEnabled}"
199200
HorizontalScrollBarVisibility="Auto"
200201
Template="{StaticResource ScrollViewerTemplate}"
201202
VerticalScrollBarVisibility="Disabled">
@@ -311,6 +312,7 @@
311312

312313
<Style BasedOn="{StaticResource MetroTabControl}" TargetType="{x:Type TabControl}">
313314
<Setter Property="Controls:TabControlHelper.Transition" Value="Left" />
315+
<Setter Property="Controls:ScrollViewerHelper.IsHorizontalScrollWheelEnabled" Value="True" />
314316
<Setter Property="Template" Value="{StaticResource HorizontalAnimatedSingleRowTabControl}" />
315317
<Style.Triggers>
316318
<Trigger Property="TabStripPlacement" Value="Top">

src/MahApps.Metro/MahApps.Metro/Styles/Controls.Scrollbars.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
</Style>
231231

232232
<Style x:Key="MetroScrollViewer" TargetType="{x:Type ScrollViewer}">
233-
<Setter Property="Controls:ScrollBarHelper.VerticalScrollBarOnLeftSide" Value="False" />
233+
<Setter Property="Controls:ScrollViewerHelper.VerticalScrollBarOnLeftSide" Value="False" />
234234
<Setter Property="Template">
235235
<Setter.Value>
236236
<ControlTemplate TargetType="{x:Type ScrollViewer}">
@@ -275,7 +275,7 @@
275275
Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
276276
</Grid>
277277
<ControlTemplate.Triggers>
278-
<Trigger Property="Controls:ScrollBarHelper.VerticalScrollBarOnLeftSide" Value="True">
278+
<Trigger Property="Controls:ScrollViewerHelper.VerticalScrollBarOnLeftSide" Value="True">
279279
<Setter TargetName="PART_HorizontalScrollBar" Property="Grid.Column" Value="1" />
280280
<Setter TargetName="PART_ScrollContentPresenter" Property="Grid.Column" Value="1" />
281281
<Setter TargetName="PART_VerticalScrollBar" Property="Grid.Column" Value="0" />

src/MahApps.Metro/MahApps.Metro/Themes/HamburgerMenu.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
150150
</Grid>
151151
<ControlTemplate.Triggers>
152-
<Trigger Property="controls:ScrollBarHelper.VerticalScrollBarOnLeftSide" Value="True">
152+
<Trigger Property="controls:ScrollViewerHelper.VerticalScrollBarOnLeftSide" Value="True">
153153
<Setter TargetName="PART_HorizontalScrollBar" Property="Grid.Column" Value="1" />
154154
<Setter TargetName="PART_ScrollContentPresenter" Property="Grid.Column" Value="1" />
155155
<Setter TargetName="PART_VerticalScrollBar" Property="HorizontalAlignment" Value="Left" />
@@ -254,7 +254,7 @@
254254
Grid.Row="1"
255255
HorizontalAlignment="Stretch"
256256
VerticalAlignment="Stretch"
257-
controls:ScrollBarHelper.VerticalScrollBarOnLeftSide="{TemplateBinding VerticalScrollBarOnLeftSide}"
257+
controls:ScrollViewerHelper.VerticalScrollBarOnLeftSide="{TemplateBinding VerticalScrollBarOnLeftSide}"
258258
HorizontalScrollBarVisibility="Disabled"
259259
Style="{StaticResource HamburgerScrollViewerStyle}"
260260
VerticalScrollBarVisibility="Auto">

src/MahApps.Metro/MahApps.Metro/Themes/MetroAnimatedSingleRowTabControl.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</Grid.RowDefinitions>
1818
<ScrollViewer x:Name="HeaderPanelScroll"
1919
Grid.Row="0"
20+
Controls:ScrollViewerHelper.IsHorizontalScrollWheelEnabled="{TemplateBinding Controls:ScrollViewerHelper.IsHorizontalScrollWheelEnabled}"
2021
Margin="{TemplateBinding TabStripMargin}"
2122
Panel.ZIndex="1"
2223
HorizontalScrollBarVisibility="Auto"
@@ -131,6 +132,7 @@
131132

132133
<Style BasedOn="{StaticResource MetroTabControl}" TargetType="{x:Type Controls:MetroAnimatedSingleRowTabControl}">
133134
<Setter Property="Controls:TabControlHelper.Transition" Value="Left" />
135+
<Setter Property="Controls:ScrollViewerHelper.IsHorizontalScrollWheelEnabled" Value="True" />
134136
<Setter Property="Template" Value="{StaticResource HorizontalMetroAnimatedSingleRowTabControl}" />
135137
<Style.Triggers>
136138
<Trigger Property="TabStripPlacement" Value="Top">

0 commit comments

Comments
 (0)