2

编辑:问题的原始前提不正确,因此修改了问题:

基本上我希望一个按钮只有在鼠标悬停在包含用户控件上时才可见。这是我所拥有的简化版本:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyNamespace.MyUserControl"
    x:Name="myUserControl">
    <Textbox>Some Text</Textbox>
    <Button Visibility="{Binding ElementName=myUserControl, Path=IsMouseOver, Converter={StaticResource mouseOverVisibilityConverter}}" />
</UserControl>

如果鼠标在文本框上,但在用户控件中的其他任何地方都不起作用。

4

3 回答 3

6

一旦 Thomas 指出了我原来问题中的错误假设,我就修改了这个问题,这导致我发现它在这篇文章中不起作用的真正原因。

基本上,用户控件有一个空背景(与透明相反),这显然使其对鼠标不可见,即使 IsHitTestVisible 设置为 true,所以解决方案是将 Background="Transparent" 添加到用户控件。

于 2009-06-06T17:58:28.113 回答
2

我意识到 UserControl 没有 IsMouseOver 属性

但它确实...... IsMouseOver 在 UIElement 类中定义,UserControl(间接)从该类继承

于 2009-06-06T15:54:28.873 回答
1

您可以在派生类中实现该属性。我以前不得不做这种事情。

Private _IsMouseOver As Boolean = False

Protected Overrides Sub OnMouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
     _IsMouseOver = True
     MyBase.OnMouseEnter(sender, e)
End Sub

Protected Overrides Sub OnMouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
     _IsMouseOver = False
     MyBase.OnMouseLeave(sender, e)
End Sub

Public ReadOnly Property IsMouseOver As Boolean()
    Get
        Return _IsMouseOver
    End Get
End Property
于 2009-06-06T15:47:31.133 回答