如何在WPF中鼠标悬停时显示悬停信息气泡?

Edw*_*uay 15 c# wpf xaml mouseover hover

我想在鼠标悬停在TextBlock上时出现文本气泡.

下面的代码是最接近我能得到,但它只是注入文本TextBox.Text本身并改变颜色.我想在鼠标悬停期间在原始文本块上方放置一个例如Border/StackPanel/TextBlock 浮动在不同的图层上.

如何使用首字母缩略词标记制作类似于网络体验的悬停面板?

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.";
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Mua*_*Dib 47

有几种方法可以做到,一种方法是使用自定义样式的工具提示.或者,您可以使用弹出控件,第三个选项是使用装饰器.

我的直觉说你想要一个工具提示,所以.

<TextBlock ToolTip="stuff, could even be a custom control, etc" Text="my text" />
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用ToolTipService附加属性为所述工具提示设置各种选项,从延迟到工具提示位置