Use the Mouse Wheel action methods that are defined by the IScrollInfo interface
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ScrollViewer_Methods.Window1"
Title="ScrollViewer IScrollInfo Sample"
Loaded="onLoad">
<DockPanel>
<TextBlock DockPanel.Dock="Top" FontSize="20" FontWeight="Bold" Margin="10">IScrollInfo Interface Methods</TextBlock>
<StackPanel DockPanel.Dock="Left" Width="150">
<Button Click="spMouseWheelDown">MouseWheelDown</Button>
<Button Click="spMouseWheelUp">MouseWheelUp</Button>
<Button Click="spMouseWheelLeft">MouseWheelLeft</Button>
<Button Click="spMouseWheelRight">MouseWheelRight</Button>
</StackPanel>
<Border BorderBrush="Black" Background="White" BorderThickness="2" Width="500" Height="500">
<ScrollViewer Name="sv1" CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<StackPanel Name="sp1">
<Button>Button 1</Button>
<Button>Button 5</Button>
<Rectangle Width="700" Height="500" Fill="Green"/>
<TextBlock>Rectangle 3</TextBlock>
</StackPanel>
</ScrollViewer>
</Border>
</DockPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Text;
namespace ScrollViewer_Methods
{
public partial class Window1 : Window
{
private void onLoad(object sender, System.EventArgs e)
{
((IScrollInfo)sp1).CanVerticallyScroll = true;
((IScrollInfo)sp1).CanHorizontallyScroll = true;
((IScrollInfo)sp1).ScrollOwner = sv1;
}
private void spMouseWheelDown(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).MouseWheelDown();
}
private void spMouseWheelUp(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).MouseWheelUp();
}
private void spMouseWheelLeft(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).MouseWheelLeft();
}
private void spMouseWheelRight(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).MouseWheelRight();
}
}
}
Related examples in the same category