子控件阻止父控件的事件处理程序
我有一个用户控件,它有一个已注册的 MouseDown 事件处理程序。用户控件有一个带有 ItemsControl 和 TextBlock 的 ScrollViewer。 ItemsControl 和 TextBlock 的可见性由 MouseDown 事件处理程序切换,因此一次只有其中之一可见。当我单击 TextBlock 但不是在 ItemsControl 中时,事件处理程序被正确调用。
如果我将 ItemsControl 上的“IsHitTestVisible”设置为 false,视图的事件处理程序就会暴露,但我无法滚动。
有人可以建议一条出路吗?
代码有点像这样:
<Grid>
<Border>
<ScrollViewer>
<TextBlock>
</ScrollViewer>
<Border>
<Border>
<Grid>
<ItemsControl/> <!-- Has a ScrollViewer in template to show scroll bar -->
</Grid>
<Border>
</Grid>
I have a user control which has a registered MouseDown eventhandler. The user control has a ScrollViewer with an ItemsControl and a TextBlock. The visibility of the ItemsControl and TextBlock is toggled by the MouseDown eventhandler so at a time only one of them is visible. The eventhandler is invoked correctly when I click the TextBlock but not in the ItemsControl.
If I set the "IsHitTestVisible" on the ItemsControl to false the view's eventhandler gets exposed but I'm not able to scroll.
Can someone suggest a way out please?
Code is somewhat like this:
<Grid>
<Border>
<ScrollViewer>
<TextBlock>
</ScrollViewer>
<Border>
<Border>
<Grid>
<ItemsControl/> <!-- Has a ScrollViewer in template to show scroll bar -->
</Grid>
<Border>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的 UI 场景对我来说有点奇怪,但如果你仍然想实现它,那么你可以执行以下操作。您没有收到为 paren 引发的事件,因为 ItemsControl 将其标记为已处理,因此它不是 进一步路由。您可以订阅 PreviewMouseDown 事件而不是 MouseDown在父级中。那么无论发生什么,父级都会在子级之前收到通知。
更新
您可以执行以下操作,
这样您就可以滚动文本块和项目。 ItemsControl自己的滚动条将不会被使用。
更新2
如果您想拥有单独的ScrollViewers,那么另一种可能性如下(尽管我认为某些边框和网格确实不需要。仅当它们在您的代码片段中起作用时)
Your UI scenario sounds kinda strange to me, but if you still want to implement it then you can do the following. You are not getting event raised for the paren becaus ItemsControl marks it as handled, so it is not routed further. You can subscribe to PreviewMouseDown event instead of MouseDown in the parent. Then no matter what the parent will receive notification before it's children.
UPDATE
You can do the following
So you will have scroll for text block and for items. ItemsControl's own scroll bars will not be used.
UPDATE 2
If you want to have separate ScrollViewers, then another possibility can be the following (although I think some of borders and grids are really unneeded. Only if they are functional in your snippet)