HorizontalScroll 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package HorizontalScroll --version 1.0.0
NuGet\Install-Package HorizontalScroll -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HorizontalScroll" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HorizontalScroll --version 1.0.0
#r "nuget: HorizontalScroll, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HorizontalScroll as a Cake Addin
#addin nuget:?package=HorizontalScroll&version=1.0.0

// Install HorizontalScroll as a Cake Tool
#tool nuget:?package=HorizontalScroll&version=1.0.0

horizontalscroll-banner

Adds better horizontal scrolling support to WPF, and some useful behaviors for improving ScrollViewer.

NuGet Version
HorizontalScroll NuGet Status

Many newer high-end mice have scroll wheels that can be tilted left or right to scroll horizontally, but WPF doesn't provide any support for them. HorizontalScroll provides attached events to receive these messages on a per-control basis, just like the built-in PreviewMouseWheel & MouseWheel events.

Usage

No xmlns declaration is required to use HorizontalScroll in XAML.

Table of Contents

  1. Using HorizontalScrollBehavior
    1. Application-Wide Support
  2. Events

Behaviors

HorizontalScrollBehavior can be attached to any ScrollViewer control to add support for horizontal scrolling by tilting the scroll wheel, or by holding the Shift key while scrolling up/down.
You can attach it to a specific ScrollViewer entirely in XAML:

<ScrollViewer xmlns:i="http://schemas.microsoft.com/xaml/behaviors">
    <i:Interaction.Behaviors>
        <HorizontalScrollBehavior Magnitude="0.5" />
        
    </i:Interaction.Behaviors>
</ScrollViewer>

Scrollable WPF controls like ListBox, ListView, TreeView, DataGrid, etc. are implemented using a ScrollViewer, so you can use HorizontalScrollBehavior to add support for them as well. While you could use a ControlTemplate, it's much more maintainable - not to mention faster and easier - to attach it in the code-behind with a style and an EventSetter instead:

<ListBox>
    <ListBox.Resources>
        <Style TargetType="{x:Type ScrollViewer}">
            <EventSetter Event="Loaded" Handler="ScrollViewer_Loaded" />
        </Style>
    </ListBox.Resources>
</ListBox>
private void ScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    // Tip: You can set the Magnitude property (to a value or binding) in the HorizontalScrollBehavior constructor.
    Interaction
        .GetBehaviors((ScrollViewer)sender)
        .Add(new HorizontalScrollBehavior( /* magnitude: 0.5 | magnitudeBinding: new Binding() */ ));
}

Enabling Application-Wide Support

The previous example showed how to attach HorizontalScrollBehavior to a ListBox's internal ScrollViewer, but that approach can also be used for an entire window or application. The exact same code as the previous example will work just fine, but if you want to make your own ScrollViewer style later on you may want one with a key so you can inherit from it:

<ListBox>
    <ListBox.Resources>

        
        <Style x:Key="AttachScrollBehaviorStyle" TargetType="{x:Type ScrollViewer}">
            <EventSetter Event="Loaded" Handler="ScrollViewer_Loaded" />
        </Style>

        
        <Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource AttachScrollBehaviorStyle}" />

    </ListBox.Resources>
</ListBox>
private void ScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    // Tip: You can set the Magnitude property in the HorizontalScrollBehavior constructor.
    Interaction
        .GetBehaviors((ScrollViewer)sender)
        .Add(new HorizontalScrollBehavior(/* 0.5 | new Binding() | new MultiBinding() */));
}

Events

Event Handler Type EventArgs Type
HorizontalScroll.PreviewMouseWheelTilt MouseWheelEventHandler MouseWheelEventArgs
HorizontalScroll.MouseWheelTilt MouseWheelEventHandler MouseWheelEventArgs

Example

<TextBox
    x:Name="MyTextBox"
    HorizontalScroll.PreviewMouseWheelTilt="MyTextBox_PreviewMouseWheelTilt"
    HorizontalScroll.MouseWheelTilt="MyTextBox_PreviewMouseWheelTilt" />
private void TextBox_PreviewMouseWheelTilt(object sender, MouseWheelEventArgs e)
{
    // do something...
    // setting e.Handled to true here will prevent the MouseWheelTilt event from firing.
}
private void TextBox_MouseWheelTilt(object sender, MouseWheelEventArgs e)
{
    // do something...
}
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 214 12/11/2023
1.0.3 101 12/10/2023
1.0.2 100 12/8/2023
1.0.1 121 12/6/2023
1.0.0 105 12/5/2023
0.1.1 102 12/5/2023
0.1.0 104 12/5/2023
0.0.0 117 12/5/2023