2009-12-03 112 views
8

我想讓文字泡泡在鼠標超過時出現TextBlock如何讓鼠標懸停在WPF上出現懸停信息氣泡?

以下代碼是我能夠得到的最接近的,但它只是將文本注入TextBox.Text本身並更改顏色。我想要一個例如在鼠標懸停期間浮動在不同層上的原始文本塊上方的Border/StackPanel/TextBlock。

如何製作類似於acronym tag的網頁體驗的懸停面板?

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Media; 

namespace TestHover29282 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      TextBlock tb = new TextBlock(); 
      tb.Text = "test"; 

      tb.MouseEnter += new MouseEventHandler(tb_MouseEnter); 
      tb.MouseLeave += new MouseEventHandler(tb_MouseLeave); 

      MainStackPanel.Children.Add(tb); 
     } 

     void tb_MouseLeave(object sender, MouseEventArgs e) 
     { 
      TextBlock tb = sender as TextBlock; 
      tb.Background = new SolidColorBrush(Colors.Transparent); 
      tb.Text = "test"; 
     } 

     void tb_MouseEnter(object sender, MouseEventArgs e) 
     { 
      TextBlock tb = sender as TextBlock; 
      tb.Background = new SolidColorBrush(Colors.Orange); 
      tb.Text += " - this should be in a popup bubble."; 
     } 

    } 
} 

回答

35

夫婦的你能做到這一點的方式,一個使用工具提示自定義樣式。 替代地,您可以使用彈出控件,第三個選項是使用裝飾器。

我的直覺說你想要一個tooltip,tho。

<TextBlock ToolTip="stuff, could even be a custom control, etc" Text="my text" /> 

,那麼你可以使用ToolTipService可連接屬性來設置各種選項提示說,從延遲提示位置