【问题标题】:Invert scroll direction of ScrollViewer in an ItemsControl在 ItemsControl 中反转 ScrollViewer 的滚动方向
【发布时间】:2016-05-17 12:05:25
【问题描述】:

无论出于何种原因,我有一个在 Y 坐标中翻转的 ItemsControl,其中我有一个 ScrollViewer

<ItemsControl ...>

    ...

    <ItemsControl.LayoutTransform>
        <ScaleTransform ScaleY="-1" />
    </ItemsControl.LayoutTransform>

    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer Focusable="False" Padding="{TemplateBinding Control.Padding}">
                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>

    ...

</ItemsControl>

每个 DataTemplate 都有类似的转换,因此文本看起来正确。

由此产生的一个问题是滚动反转。反正有反转滚动方向吗?或者以某种方式自己进行计算,而不是默认的滚动行为。我怎样才能做到这一点?

【问题讨论】:

    标签: .net wpf scroll


    【解决方案1】:

    如果将 ItemsControl 的 LayoutTransform 移到 ItemsPresenter 会怎样?那么scrollviewer就在翻转之外了。

    <ItemsControl ...>
        ...
        <ItemsControl.Template>
            <ControlTemplate>
                <ScrollViewer Focusable="False" Padding="{TemplateBinding Control.Padding}">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}">
                        <ItemsPresenter.LayoutTransform>
                            <ScaleTransform ScaleY="-1" />
                        </ItemsPresenter.LayoutTransform>
                    </ItemsPresenter>
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
        ...
    </ItemsControl>
    

    好的...所以这对你不起作用。

    如何制作自己的滚动查看器并覆盖 OnPreviewMouseWheel 以反转鼠标滚轮的增量变化?

    public class ScrollViewerInvert : System.Windows.Controls.ScrollViewer
    {
        protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
        {
            if (e.Delta != 0)
                e = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta * -1);
            base.OnPreviewMouseWheel(e);
        }
    }
    

    【讨论】:

    • 似乎无法正常工作。首先,滚动条现在错误地出现在顶部(但内容在底部)。其次,滚动仍然是倒置的。
    • @LeoNatan 查看编辑。也许我不明白你的问题?
    • 赞成创建新的 EventArgs 对象,因为它是只读的 Delta 属性。
    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多