子控件阻止父控件的事件处理程序

发布于 2024-11-09 11:26:21 字数 626 浏览 0 评论 0原文

我有一个用户控件,它有一个已注册的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2024-11-16 11:26:21

你的 UI 场景对我来说有点奇怪,但如果你仍然想实现它,那么你可以执行以下操作。您没有收到为 paren 引发的事件,因为 ItemsControl 将其标记为已处理,因此它不是 进一步路由。您可以订阅 PreviewMouseDown 事件而不是 MouseDown在父级中。那么无论发生什么,父级都会在子级之前收到通知。

更新

您可以执行以下操作,

<ScrollViewer>
    <Grid MouseDown="HandleMouseDown">
        <TextBlock />
        <ItemsControl IsHitTestVisible="false"/>
    </Grid>
</ScrollViewer>

这样您就可以滚动文本块和项目。 ItemsControl自己的滚动条将不会被使用。

更新2

如果您想拥有单独的ScrollViewers,那么另一种可能性如下(尽管我认为某些边框和网格确实不需要。仅当它们在您的代码片段中起作用时)

  <Grid>
    <Border>
      <ScrollViewer>
        <TextBlock MouseDown="HandleMouseDown">
      </ScrollViewer>
    <Border>
    <Border>
      <Grid>
        <ScrollViewer>
          <Grid MouseDown="HandleMouseDown">
            <ItemsControl IsHitTestVisible="false"/>
          </Grid>
        </ScrollViewer>
      </Grid>
    <Border>
  </Grid>

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

<ScrollViewer>
    <Grid MouseDown="HandleMouseDown">
        <TextBlock />
        <ItemsControl IsHitTestVisible="false"/>
    </Grid>
</ScrollViewer>

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)

  <Grid>
    <Border>
      <ScrollViewer>
        <TextBlock MouseDown="HandleMouseDown">
      </ScrollViewer>
    <Border>
    <Border>
      <Grid>
        <ScrollViewer>
          <Grid MouseDown="HandleMouseDown">
            <ItemsControl IsHitTestVisible="false"/>
          </Grid>
        </ScrollViewer>
      </Grid>
    <Border>
  </Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文