2009-06-06 77 views
2

编辑:问题的最初前提是不正确,从而修正了问题:(WPF)你如何绑定到IsMouseOver在用户控件

基本上我想有一个按钮,是可见的,只有当鼠标悬停包含用户控件。下面是我有什么简化正矢量:

<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> 

如果鼠标在文本框其中一期工程,而不是其他任何地方的用户控件。

回答

6

托马斯在我的原始问题中指出了错误的假设,这让我发现它在this post中不起作用的真正原因,我修改了这个问题。

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

+0

谢谢......我试图弄清楚为什么我的一个边框的IsMouseOver属性是错误的,即使当鼠标直接打开时也是如此。非常令人沮丧的弄清楚,但它现在非常有意义...... :) – Siege 2011-01-20 14:29:46

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 
2

我意识到,用户控件没有IsMouseOver属性

但它确实...... IsMouseOver在UIElement类中定义,从用户控件(间接)继承

+0

感谢您指出我错误的假设,因为谷歌搜索wpf ismouseover只返回msdn上的IInputElement。 UIElement版本甚至不在前两页。 – Davy8 2009-06-06 18:01:21