WPF 网格 IsMouseOver 属性 [英] WPF Grid IsMouseOver Property

查看:54
本文介绍了WPF 网格 IsMouseOver 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 网格 有一个"IsMouseOver"属性,您可以在网格的样式触发器中使用.

The WPF Grid has an "IsMouseOver" property that you can use in the Grid's Style's Triggers.

我的问题是IsMouseOver"属性仅在鼠标位于网格本身内的某个控件(即按钮或组合框)上时才会更改.

My problem is that the "IsMouseOver" property only changes if the mouse is over some control (ie a Button, or ComboBox) within the Grid itself.

例如:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="25" />
    <ColumnDefinition />
  </Grid.ColumnDefinitions>

  <Button Grid.Column="1">A Button</Button>

  <Grid.Style>
    <Style TargetType="{x:Type Grid}">
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="False">
          <Setter Property="Opacity" Value="0.5"></Setter>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Opacity" Value="1"></Setter>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Grid.Style>
</Grid>

上面的 Grid 及其内容将以一半不透明度显示,以便您可以看到控件.

The above Grid and it's contents will be displayed in half opacity so that you can see the controls.

您会注意到,如果您将鼠标悬停在第一列(不包含任何内容)上,不透明度将不会设置为完整.

You will notice that the opacity will not be set to full if you hover over the first column (which doesn't contain anything).

但是,如果您将鼠标悬停在第二列中的按钮上,则不透明度设置为完整.

However the opacity will be set to full if you hover over the button in the second column.

在我的应用程序中,我为其设置触发器的网格显示在图像控件的顶部.我不希望在鼠标悬停在图像上之前显示网格......换句话说,由于网格在图像的顶部,我不希望在鼠标悬停之前显示网格网格(网格中的任何位置),因为网格位于图像的顶部.

In my application, the Grid that I'm setting the triggers for is being displayed on top of an image control. I do not want the Grid to be displayed until the mouse is hovering over the image... In other words, since the Grid is on top of the image, I don't want the grid to be displayed until the mouse is hovering over the Grid (anywhere in the grid) because the Grid is on top of the image.

有谁知道如何做到这一点?

Does anyone know how to accomplish this?

谢谢!

-弗林妮

推荐答案

您的问题是 Grid 本身无法进行命中测试,因为它没有背景.试试这个:

Your problem is that the Grid itself is not hit-testable because it has no background. Try this instead:

<Grid Background="Transparent">

这篇关于WPF 网格 IsMouseOver 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆