滚动浏览器的儿童元素防止滚动鼠标滚轮? [英] Child elements of scrollviewer preventing scrolling with mouse wheel?

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

问题描述

 我有一个问题,让鼠标滚轮滚动到以下XAML中, >< ScrollViewer 
Horizo​​ntalScrollBarVisibility =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 =扩展
背景=透明
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 <从集合中读取的code>可以用具有任意位置,大小等的 TextBox es表示。在 ListBox 的样式区域太大而不能一次显示的情况下,水平和垂直滚动是可能的,但只能使用滚动条。



为了更好的人体工程学和健全性,应该可以使用鼠标滚轮滚动,通常 ScrollViewer 会自动处理它,但 ListBox 似乎正在处理这些事件,使得父 ScrollViewer 永远不会看到它们。到目前为止,我只能为 ListBox 或父级的 IsHitTestVisible = False 设置滚动滚动 Grid ,但当然没有一个子元素的鼠标事件在此之后工作。



我可以做些什么来确保 ScrollViewer 看到鼠标滚轮事件,同时保留其他孩子元素?



编辑:我刚刚了解到 ListBox 有一个内置的 ScrollViewer ,这可能是从父 ScrollViewer 并指定控件模板可以禁用它。

解决方案

指定一个 ControlTemplate for Listbox 不包含 ScrollViewer 解决问题。有关详细信息,请参阅此答案和这两个MSDN页面:



ControlTemplate



ListBox样式和模板


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>

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.

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.

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

Edit: 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.

解决方案

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

ListBox Styles and Templates

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

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