UIElement.IsMouseOver UIElement.IsMouseOver UIElement.IsMouseOver UIElement.IsMouseOver Property

定义

获取一个值,该值指示鼠标指针是否位于此元素(包括可视化树中的子元素)的上方。Gets a value indicating whether the mouse pointer is located over this element (including child elements in the visual tree). 这是依赖项属性。This is a dependency property.

public:
 property bool IsMouseOver { bool get(); };
public bool IsMouseOver { get; }
member this.IsMouseOver : bool
Public ReadOnly Property IsMouseOver As Boolean

属性值

如果鼠标指针位于该元素或其子元素上方,则为 true;否则为 falsetrue if mouse pointer is over the element or its child elements; otherwise, false. 默认值为 falseThe default is false.

实现

示例

下面的示例将此属性作为Trigger <Style 的一部分的属性类型引用。触发器 > 块。The following example references this property as the property type of a Trigger, part of a <Style.Triggers> block. 如果鼠标位于控件上方, 控件文本将变为蓝色, 光标将变为手形。If the mouse is over the control, the control text turns blue and the cursor becomes a hand.

<Trigger Property="IsMouseOver" Value="true">
  <Setter Property="Foreground" Value="Blue" />
  <Setter Property="Cursor" Value="Hand"/>
</Trigger>

注解

通常, 控件是复合的, 因此控件内的各种元素 (可视化树) 都将报告包含控件的鼠标状态。Typically, controls are composited such that the various elements inside the control (the visual tree) will all report the mouse state for the containing control. ListBox例如, 如果鼠标位于其几何图形上的true任意位置, 则样式控件将报告IsMouseOver为, 包括ListBoxItem任何。For example, a ListBox style control will report IsMouseOver as true if the mouse is anywhere over its geometry, including any ListBoxItem.

尽管不存在类似的 "IsMouseOverChanged" 事件, 但有几个类似的事件。Although an analogous "IsMouseOverChanged" event does not exist, several similar events do. 例如, 可以处理MouseEnterMouseMoveIsMouseDirectlyOverChangedFor example, you can handle MouseEnter, MouseMove, and IsMouseDirectlyOverChanged.

如果此元素捕获鼠标, 则此属性将true保留, 直到鼠标捕获丢失, 并且鼠标指针离开元素边界。If this element captures the mouse, this property remains true until mouse capture is lost and the mouse pointer leaves the element bounds.

某些控件有意捕获鼠标上某些不直接涉及鼠标的操作。Some controls deliberately capture the mouse on certain actions that do not appear to directly involve the mouse. 即使鼠标没有明显移动, 这也可能会导致IsMouseOvertrueThis can lead to IsMouseOver being true even though the mouse has not apparently moved.

依赖项属性信息Dependency Property Information

标识符字段Identifier field IsMouseOverProperty
元数据属性设置为trueMetadata properties set to true NoneNone

适用于