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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1841800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:19:50  来源:igfitidea点击:

How can I make a hover info bubble appear on mouseover in WPF?

c#wpfxamlhovermouseover

提问by Edward Tanguay

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

当鼠标悬停在TextBlock 上时,我想让文本气泡出现。

The following codeis 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 layerduring mouseover.

下面的代码是最接近我能得到,但它只是注入文本TextBox.Text本身并改变颜色。我希望在鼠标悬停期间浮动在不同图层上的原始文本块上方有一个例如 Border/StackPanel/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.";
        }

    }
}

采纳答案by Muad'Dib

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.

我的直觉说你想要一个工具提示,不过。

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

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

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