【发布时间】:2012-07-12 19:22:14
【问题描述】:
对于可能的菜鸟问题,我很抱歉,我对 C# 和 WPF 还是很陌生。
我创建了一个带有一些控件的窗口。他们都填写了工具提示。我想在窗口底部有一个专用区域(一个 TextBlock)来显示这些提示而不是工具提示气球。我已经看到了类似的解决方案:
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
EventManager.RegisterClassHandler(typeof(FrameworkElement), FrameworkElement.MouseEnterEvent, new MouseEventHandler(MyMouseEventHandler));
}
private void MyMouseEventHandler(object sender, MouseEventArgs e)
{
// To stop the tooltip from appearing, mark the event as handled
// But not sure if it is really working
e.Handled = true;
FrameworkElement source = e.OriginalSource as FrameworkElement;
if (null != source && null != source.ToolTip)
{
// This really disables displaying the tooltip. It is enough only for the current element...
source.SetValue(ToolTipService.IsEnabledProperty, false);
// Instead write the content of the tooltip into a textblock
textBoxDescription.Text = source.ToolTip.ToString();
}
}
文本块没有数据绑定,只是简单地从代码中设置文本。
我的问题是这在第一次运行时工作正常,当对话框使用 ShowDialog() 方法启动时。但是在关闭(或隐藏)并再次显示后 textBoxDescription 不再更新。该事件被引发和处理,我可以在调试时看到,该控件甚至到达设置 textBoxDescription.Text 的行,只是 TextBlock 不再更新。我尝试将 TextBlock 替换为 TextBox,但结果相同。
有没有办法强制 TextBlock 更新?为什么我需要它?为什么它第一次起作用?
非常感谢。
【问题讨论】:
-
我用几个按钮 TextBlock (textBoxDescription) 创建了 xaml 文件,它正在与您的代码隐藏一起工作。你能添加你的 xaml 文件吗?
-
为什么要重用对话框?可能有一些其他状态会因重用而破坏。更好的方法是实例化一个新的
MyWindow实例,然后在其上调用ShowDialog()。
标签: c# wpf tooltip textblock mouseenter