【问题标题】:Allow mouse events to pass through a grid in UserControl允许鼠标事件通过 UserControl 中的网格
【发布时间】:2017-09-19 06:31:22
【问题描述】:

我已经看到这个link 来解决我的问题,但这对我不起作用,因为我的布局中有两个网格,我需要其中一个获得鼠标事件,另一个没有。第一个网格就像一个带有按钮的标题栏,我可以在其中移动我的窗口。有没有人有任何解决方案或建议?

我的 Xaml

 <UserControl ...>
    <Grid>
        <Grid.RowDefinitions>
           <RowDefinition Height="Auto"/>
           <RowDefinition Height="*"/>
       </Grid.RowDefinitions>

       <Grid Grid.Row="0">

             ....

        </Grid>

        <Grid Grid.Row="1" 
              behaviors:PopupBehavior.IsPopupEventTransparent="True">

              .....

        </Grid>
   </Grid></UserControl>

【问题讨论】:

  • 能否请您出示您的 xaml,以便我们帮助您解决您的问题?

标签: c# wpf


【解决方案1】:

如果在根元素上将IsHitTestVisible 属性设置为false,则不能在其任何子元素上将其设置为true。如您所见,这个问题从未得到答案How can you set IsHitTestVisible to True on a child of something where it's set to false?

因此您可以忘记使用 HitTestVisible 子级传递 UserControl。

【讨论】:

    【解决方案2】:

    如果您想忽略任何 WPF UI 元素上的鼠标输入,您可能只需将其 IsHitTestVisible 属性设置为 false

    例子:

    <Grid>
      <Button Click="OnButtonClick" Content="The Button" />
      <Border Background="Red" Opacity="0.5" IsHitTestVisible="False" />
    </Grid>
    

    红色半透明边框将显示在按钮前面。尽管如此,按钮仍将接收点击事件,因为对边框禁用了命中测试。

    延伸阅读:MSDN

    【讨论】:

    • 谢谢,但我也需要鼠标事件通过我的 UserControl,但只是在这个网格中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多