Reputation: 426
I have a stackpanel wrapped with a scrollviewer. inside the stackpanel I've some grids and inside the grids there're stackpanels again and some tiles control by MahApps Metro.
the scrollviewer is working fine if I drag the scrollbar. but mousewheel not working. may be some control is stealing the mousewheel action but I can't feagure out which one. I've tried to scroll mouse wheel while focusing on the scrollbar. but it's still not working.
<ScrollViewer x:Name="TS" Grid.Row="1" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" Background="Transparent">
<StackPanel x:Name="TilesPanel" VerticalAlignment="Top" HorizontalAlignment="Stretch" Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type Grid}">
<Setter Property="Margin" Value="0,50,0,0"/>
</Style>
</StackPanel.Resources>
<Separator Background="{x:Null}" Width="110"></Separator>
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type Grid}">
<Setter Property="Margin" Value="10,0,0,0"/>
</Style>
</StackPanel.Resources>
<Grid Height="260">
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Margin" Value="0,0,0,10"/>
</Style>
</StackPanel.Resources>
<StackPanel Width="247" Height="119">
<Custom:Tile x:Name="Mail" Margin="0" Width="auto" d:LayoutOverrides="Height">
<Image Stretch="Fill" Source="Res/AppTiles/Mail.png"/>
</Custom:Tile>
</StackPanel>
//and it goes on like this//
</grid>
</stackpanel>
<Separator Background="{x:Null}" Width="50"/>
</Grid>
</StackPanel>
</ScrollViewer>
Is the grid that's coming in the way? Or is there any other way to use mousewheel in Horizontal Scrollviewer? I just can't figure out. please help.
Upvotes: 1
Views: 12557
Reputation: 426
Yes I figured that out. and solved it in the minimal way like this.
<ScrollViewer x:Name="TS" Grid.Row="1" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" Background="Transparent" PreviewMouseWheel="TS_PreviewMouseWheel">
added PreviewMouseWheel="TS_PreviewMouseWheel"
and in the code behind,
private void TS_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
TS.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
ScrollViewer scrollviewer = sender as ScrollViewer;
if (e.Delta > 0)
{
scrollviewer.LineLeft();
}
else
{
scrollviewer.LineRight();
}
e.Handled = true;
}
thanks anyway.
Upvotes: 10
Reputation: 13679
usually a mouse have only one scroll wheel which is assigned for vertical scroll. I assume same for your case.
based on your code it seems like you want to perform a horizontal scroll by the mouse wheel.
I propose a solution using Attached properties
sample xaml
<ScrollViewer x:Name="TS"
Grid.Row="1"
HorizontalAlignment="Stretch"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Disabled"
CanContentScroll="False"
PanningMode="HorizontalOnly"
SnapsToDevicePixels="True"
Background="Transparent"
l:ScrollViewerExtensions.IsHorizontalScrollOnWheelEnabled="true">
I have attached ScrollViewerExtensions.IsHorizontalScrollOnWheelEnabled
to the scroll viewer which will enable horizontal scrolling via mouse wheel. also remember to set CanContentScroll="False"
, it is needed in your case.
ScrollViewerExtensions class
namespace CSharpWPF
{
class ScrollViewerExtensions : DependencyObject
{
public static bool GetIsHorizontalScrollOnWheelEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsHorizontalScrollOnWheelEnabledProperty);
}
public static void SetIsHorizontalScrollOnWheelEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsHorizontalScrollOnWheelEnabledProperty, value);
}
// Using a DependencyProperty as the backing store for IsHorizontalScrollOnWheelEnabled. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsHorizontalScrollOnWheelEnabledProperty =
DependencyProperty.RegisterAttached("IsHorizontalScrollOnWheelEnabled", typeof(bool), typeof(ScrollViewerExtensions), new PropertyMetadata(false, OnIsHorizontalScrollOnWheelEnabledChanged));
private static void OnIsHorizontalScrollOnWheelEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ScrollViewer sv = d as ScrollViewer;
if ((bool)e.NewValue)
sv.PreviewMouseWheel += sv_PreviewMouseWheel;
else
sv.PreviewMouseWheel -= sv_PreviewMouseWheel;
}
static void sv_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
ScrollViewer scrollviewer = sender as ScrollViewer;
if (e.Delta > 0)
scrollviewer.LineLeft();
else
scrollviewer.LineRight();
e.Handled = true;
}
}
}
whole idea is to listen to the mouse wheel and scroll left or right based on the wheel delta (rotation)
Upvotes: 5