【发布时间】:2020-05-18 12:23:42
【问题描述】:
我在 Scroll Viewer 中有一个 ListBox。 Scroll Viewer 已附加一个 Scroll-Changed 侦听器,我在其中放置了一个:
MessageBox.Show("Something Happened!");
这是我的 WPF 代码:
<ScrollViewer ScrollChanged="ScrollViewer_ScrollChanged" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled">
<ListBox>
<ListBox.Items>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
<ListBoxItem Content="item 2"/>
</ListBox.Items>
</ListBox>
</ScrollViewer>
现在的问题是,当我滚动 ListBox 时,不知何故 ScrollViewer 的 'ScrollChanged' 事件通过向我显示 MessageBox => 发生了一些事情来触发!
我尝试过启用或禁用 HorizontalScrollBar,但同样的事情发生了...... 我的 VerticalScrollBar 现在已禁用,我将其设置为“隐藏”,但现在它也隐藏了 ListBox 的垂直滚动条,我也无法通过鼠标滚轮滚动 ListBox
提前谢谢... ????
【问题讨论】:
-
你好,我不明白是什么问题,你监听了一个滚动事件,你不希望代码在这个事件上触发?
-
我不希望代码触发 ListBox 的 Scroll-Changed 事件。事件被分配给外层的Scroll Viewer,所以,它应该只在我们滚动Parent-ScrollViewer时触发代码,而不是当我们滚动内层ListBox时触发代码
标签: c# wpf events scrollview parent-child