子控件阻塞父亲的事件处理程序

时间:2011-05-24 16:19:41

标签: wpf wpf-controls

我有一个用户控件,它有一个注册的MouseDown事件处理程序。用户控件具有带有ItemsControl和TextBlock的ScrollViewer。 ItemsControl和TextBlock的可见性由MouseDown事件处理程序切换,因此一次只能看到其中一个。单击TextBlock而不是ItemsControl时,会正确调用eventhandler。

如果我将ItemsControl上的“IsHitTestVisible”设置为false,则视图的eventhandler会被暴露,但我无法滚动。

有人可以建议出路吗?

代码有点像这样:

<Grid>
 <Border>
  <ScrollViewer>
   <TextBlock>
  </ScrollViewer>
 <Border>
 <Border>
  <Grid>
   <ItemsControl/> <!-- Has a ScrollViewer in template to show scroll bar -->
  </Grid>
 <Border>
</Grid>

1 个答案:

答案 0 :(得分:0)

您的UI场景听起来有点奇怪,但如果您仍想实现它,那么您可以执行以下操作。你没有为paren举起活动,因为ItemsControl将其标记为已处理,因此它不是routed further。您可以在父级中订阅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>
相关问题