scrollviewer 的子元素阻止使用鼠标滚轮滚动? [英] Child elements of scrollviewer preventing scrolling with mouse wheel?

查看:39
本文介绍了scrollviewer 的子元素阻止使用鼠标滚轮滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让鼠标滚轮滚动在以下 XAML 中工作时遇到问题,为了清楚起见,我已对其进行了简化:

I'm having a problem getting mouse wheel scrolling to work in the following XAML, which I have simplified for clarity:

<ScrollViewer
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
CanContentScroll="False"
>
    <Grid
    MouseDown="Editor_MouseDown"
    MouseUp="Editor_MouseUp"
    MouseMove="Editor_MouseMove"
    Focusable="False"
    >
        <Grid.Resources>
            <DataTemplate
            DataType="{x:Type local:DataFieldModel}"
            >
                <Grid
                Margin="0,2,2,2"
                >
                    <TextBox
                    Cursor="IBeam"
                    MouseDown="TextBox_MouseDown"
                    MouseUp="TextBox_MouseUp"
                    MouseMove="TextBox_MouseMove"
                    />
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <ListBox
        x:Name="DataFieldListBox"
        ItemsSource="{Binding GetDataFields}"
        SelectionMode="Extended"
        Background="Transparent"
        Focusable="False"
        >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style
                TargetType="ListBoxItem"
                >
                    <Setter
                    Property="Canvas.Left"
                    Value="{Binding dfX}"
                    />
                    <Setter
                    Property="Canvas.Top"
                    Value="{Binding dfY}"
                    />
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</ScrollViewer>

从视觉上看,结果是一个已知大小的区域,其中从集合中读取的 DataField 可以用具有任意位置、大小等的 TextBox 表示.如果 ListBox 的样式区域"太大而无法一次全部显示,则可以进行水平和垂直滚动,但只能使用滚动条.

Visually, the result is an area of some known size where DataFields read from a collection can be represented with TextBoxes which have arbitrary position, size, et cetera. In cases where the ListBox's styled "area" is too large to display all at once, horizontal and vertical scrolling is possible, but only with the scroll bars.

为了更好的人体工程学和理智,应该可以使用鼠标滚轮滚动,并且通常 ScrollViewer 会自动处理它,但 ListBox 似乎正在处理这些事件,因此父 ScrollViewer 永远不会看到它们.到目前为止,我只能通过为 ListBox 或父 Grid 设置 IsHitTestVisible=False 来让滚轮滚动工作,但当然可以此后,子元素的鼠标事件都不起作用.

For better ergonomics and sanity, mouse wheel scrolling should be possible, and normally ScrollViewer would handle it automatically, but the ListBox appears to be handing those events such that the parent ScrollViewer never sees them. So far I have only been able to get wheel scrolling working be setting IsHitTestVisible=False for either the ListBox or the parent Grid, but of course none of the child element's mouse events work after that.

如何确保 ScrollViewer 看到鼠标滚轮事件,同时为子元素保留其他事件?

What can I do to ensure the ScrollViewer sees mouse wheel events while preserving others for child elements?

我刚刚了解到 ListBox 有一个内置的 ScrollViewer 可能会从父 ScrollViewer 窃取滚轮事件并指定控制模板可以禁用它.如果问题解决了,我会更新这个问题.

I just learned that ListBox has a built-in ScrollViewer which is probably stealing wheel events from the parent ScrollViewer and that specifying a control template can disable it. I'll update this question if that resolves the problem.

推荐答案

为不包含 ScrollViewerListbox 指定一个 ControlTemplate> 解决问题.有关详细信息,请参阅 this answer 和这两个 MSDN 页面:

Specifying a ControlTemplate for the Listbox which doesn't include a ScrollViewer solves the problem. See this answer and these two MSDN pages for more information:

ControlTemplate

列表框样式和模板

这篇关于scrollviewer 的子元素阻止使用鼠标滚轮滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆