2009-06-06 80 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