Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit c2ab38f

Browse files
committed
Make ScrollViewer hack reusable.
WPF's ScrollViewer is broken, so we need to hack around it. We already had this hack in GitHubConnectContent, so moved the hack to a central place that can be used wherever it's needed.
1 parent 5eed546 commit c2ab38f

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

src/GitHub.UI/GitHub.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="Converters\CountToVisibilityConverter.cs" />
9494
<Compile Include="Converters\DefaultValueConverter.cs" />
9595
<Compile Include="Converters\StickieListItemConverter.cs" />
96+
<Compile Include="Helpers\ScrollViewerUtilities.cs" />
9697
<Compile Include="Resources.Designer.cs">
9798
<AutoGen>True</AutoGen>
9899
<DesignTime>True</DesignTime>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Windows;
2+
using System.Windows.Input;
3+
4+
namespace GitHub.VisualStudio.UI.Helpers
5+
{
6+
/// <summary>
7+
/// Utilties for fixing WPF's broken ScrollViewer.
8+
/// </summary>
9+
public static class ScrollViewerUtilities
10+
{
11+
/// <summary>
12+
/// Fixes mouse wheel scrolling in controls that have a ScrollViewer.
13+
/// </summary>
14+
/// <param name="sender">The sender.</param>
15+
/// <param name="e">The event arguments.</param>
16+
/// <remarks>
17+
/// WPF's ScrollViewer is broken in that it doesn't pass scroll events to the parent
18+
/// control when it can't scroll any more. Add this method as an event handler to a
19+
/// control which has a ScrollViewer in its template to fix this.
20+
/// </remarks>
21+
public static void FixMouseWheelScroll(object sender, MouseWheelEventArgs e)
22+
{
23+
try
24+
{
25+
if (!e.Handled)
26+
{
27+
var control = sender as FrameworkElement;
28+
var parent = control.Parent as UIElement;
29+
30+
if (parent != null)
31+
{
32+
e.Handled = true;
33+
parent.RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
34+
{
35+
RoutedEvent = UIElement.MouseWheelEvent,
36+
Source = control,
37+
});
38+
}
39+
}
40+
}
41+
catch
42+
{
43+
}
44+
}
45+
}
46+
}

src/GitHub.VisualStudio.UI/UI/Views/GitHubConnectContent.xaml.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using GitHub.Models;
88
using System;
99
using System.Windows.Input;
10+
using GitHub.VisualStudio.UI.Helpers;
1011

1112
namespace GitHub.VisualStudio.UI.Views
1213
{
@@ -17,33 +18,9 @@ public GitHubConnectContent()
1718
InitializeComponent();
1819

1920
DataContextChanged += (s, e) => ViewModel = e.NewValue as IGitHubConnectSection;
20-
repositories.PreviewMouseWheel += Repositories_PreviewMouseWheel;
21+
repositories.PreviewMouseWheel += ScrollViewerUtilities.FixMouseWheelScroll;
2122
}
2223

23-
void Repositories_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
24-
{
25-
try
26-
{
27-
if (!e.Handled)
28-
{
29-
var uIElement = base.Parent as UIElement;
30-
31-
if (uIElement != null)
32-
{
33-
e.Handled = true;
34-
uIElement.RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
35-
{
36-
RoutedEvent = UIElement.MouseWheelEvent,
37-
Source = this
38-
});
39-
}
40-
}
41-
}
42-
catch
43-
{
44-
// TODO: Add trace logging: event handler called - who knows what can happen!
45-
}
46-
}
4724
void cloneLink_Click(object sender, RoutedEventArgs e)
4825
{
4926
cloneLink.IsEnabled = false;

0 commit comments

Comments
 (0)