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

问题描述

| 我有一个具有已注册的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>
    

解决方法

您的UI方案对我来说有点奇怪,但是如果您仍然想实现它,则可以执行以下操作。由于ItemsControl将其标记为已处理,因此不会在事件中引发事件,因此不会对其进行进一步路由。您可以订阅PreviewMouseDown事件而不是父项中的MouseDown。然后,无论父母将在孩子之前收到通知。 更新 您可以执行以下操作
<ScrollViewer>
    <Grid MouseDown=\"HandleMouseDown\">
        <TextBlock />
        <ItemsControl IsHitTestVisible=\"false\"/>
    </Grid>
</ScrollViewer>
因此,您将滚动查看文本块和项目。 ItemsControl自己的滚动条将不会使用。 更新2 如果您想拥有单独的ScrollViewer,那么另一种可能性是(尽管我认为确实不需要某些边框和网格。仅当它们在您的代码段中可用时)
  <Grid>
    <Border>
      <ScrollViewer>
        <TextBlock MouseDown=\"HandleMouseDown\">
      </ScrollViewer>
    <Border>
    <Border>
      <Grid>
        <ScrollViewer>
          <Grid MouseDown=\"HandleMouseDown\">
            <ItemsControl IsHitTestVisible=\"false\"/>
          </Grid>
        </ScrollViewer>
      </Grid>
    <Border>
  </Grid>