WPF滚动查看器鼠标滚轮不能与stackpanel一起工作
本文关键字:不能 stackpanel 一起 工作 滚动 鼠标 WPF | 更新日期: 2023-09-27 18:18:35
我有一个stackpanel包装与滚动查看器。在stackpanel里面,我有一些网格,网格里面有stackpanel和一些瓷砖控制MahApps Metro。
如果我拖动滚动条,则scrollviewer工作正常。但是鼠标转轮不工作。可能是一些控制偷走了鼠标滚轮的动作,但我不知道是哪一个。我试着滚动鼠标滚轮,同时专注于滚动条。但它仍然不能工作。
<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>
是栅格在挡道吗?或者有其他的方法来使用鼠标滚轮在水平滚动查看器?我就是想不明白。请帮助。
我明白了。然后用最小的方法解出来
<ScrollViewer x:Name="TS" Grid.Row="1" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" Background="Transparent" PreviewMouseWheel="TS_PreviewMouseWheel">
添加PreviewMouseWheel = " TS_PreviewMouseWheel "
和后面的代码
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;
}
谢谢。
通常一个鼠标只有一个滚轮,用于垂直滚动。我想你的情况也一样。
根据您的代码,您似乎想要通过鼠标滚轮执行水平滚动。
我提出了一个使用Attached properties
<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">
我已经将ScrollViewerExtensions.IsHorizontalScrollOnWheelEnabled
附加到滚动查看器,这将通过鼠标滚轮实现水平滚动。还记得设置CanContentScroll="False"
,这在你的情况下是需要的。
ScrollViewerExtensions类
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;
}
}
}
整个想法是听取鼠标滚轮并根据滚轮增量(旋转)向左或向右滚动