wpf scrollviewer鼠标滚轮不适用于stackpanel

D.K*_*.K. 0 c# mousewheel scrollviewer stackpanel wpf-4.0

我有一个用scrollviewer包装的堆栈面板。在堆栈面板内部,我有一些网格,而在网格内部,又有堆栈面板,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>
Run Code Online (Sandbox Code Playgroud)

难道是网格?还是在Horizo​​ntal Scrollviewer中使用鼠标滚轮还有其他方法吗?我只是想不通。请帮忙。

D.K*_*.K. 7

是的,我想通了。并以这样的最小方式解决它。

<ScrollViewer x:Name="TS" Grid.Row="1" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" Background="Transparent" PreviewMouseWheel="TS_PreviewMouseWheel">
Run Code Online (Sandbox Code Playgroud)

添加了 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;
 }
Run Code Online (Sandbox Code Playgroud)

不管怎么说,还是要谢谢你。


pus*_*raj 5

通常,鼠标只有一个滚轮,该滚轮分配给垂直滚轮。对于您的情况,我也认为相同。

根据您的代码,似乎您想通过鼠标滚轮执行水平滚动。

我建议使用 Attached properties

样品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">
Run Code Online (Sandbox Code Playgroud)

我已连接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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

整个想法是听鼠标滚轮,并根据滚轮增量(旋转)向左或向右滚动