2009-12-03 63 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可连接属性来设置各种选项提示说,从延迟提示位置