如何使鼠标悬停信息气泡出现在WPF上?

时间:2022-11-05 07:40:32

I want to make bubble of text appear when the mouse is over a TextBlock.

当鼠标在文本块上时,我想让文本冒泡出现。

The following code is the closest I can get but it just injects text into TextBox.Text itself and changes the color. I want to have a e.g. Border/StackPanel/TextBlock above the original textblock floating on a different layer during mouseover.

下面的代码是我能得到的最接近的代码,但它只是向文本框中注入文本。文本本身并改变颜色。我想要一个例如,边框/堆栈面板/TextBlock在鼠标悬停时浮动在不同的层上。

How can I make a hover panel similar to a web experience with the acronym tag?

如何制作一个悬停面板,类似于使用缩写标签的web体验?

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

    }
}

2 个解决方案

#1


36  

couple of ways you could do it, one use a tool tip with a custom style. alternativly, you can use a popup control, a third option would be to use an adorner.

有几种方法可以做到,一个使用工具提示和定制样式。另外,您可以使用弹出控件,第三种选择是使用装饰器。

My gut says you want a tooltip, tho.

我的直觉告诉你,你想要一个工具提示,tho。

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

you can then use the ToolTipService attachable properties to set a variety of options for said tooltip, from delays to tooltip positions

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

#2


4  

Tooltip property

工具提示属性

#1


36  

couple of ways you could do it, one use a tool tip with a custom style. alternativly, you can use a popup control, a third option would be to use an adorner.

有几种方法可以做到,一个使用工具提示和定制样式。另外,您可以使用弹出控件,第三种选择是使用装饰器。

My gut says you want a tooltip, tho.

我的直觉告诉你,你想要一个工具提示,tho。

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

you can then use the ToolTipService attachable properties to set a variety of options for said tooltip, from delays to tooltip positions

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

#2


4  

Tooltip property

工具提示属性