如何在 WPF 中的鼠标悬停时显示悬停信息气泡?
我想让当鼠标悬停在 TextBlock 上时出现文本气泡。
以下代码是我能得到的最接近的代码,但它只是将文本注入 TextBox.Text 本身并更改颜色。我希望在鼠标悬停期间在原始文本块上方有一个例如 Border/StackPanel/TextBlock 浮动在不同的层上。
如何使用 acronym 标签制作类似于 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.";
}
}
}
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.
How can I make a hover panel similar to a web experience with the 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.";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种方法可以做到这一点,一种是使用具有自定义样式的工具提示。
或者,您可以使用弹出控件,第三种选择是使用装饰器。
我的直觉告诉你你想要一个工具提示,不过。
然后,您可以使用 ToolTipService 附加项属性为所述工具提示设置各种选项,从延迟到工具提示位置
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.
you can then use the ToolTipService attachable properties to set a variety of options for said tooltip, from delays to tooltip positions
工具提示属性
Tooltip property