【问题标题】:WPF TextBlock Text updates only onceWPF TextBlock 文本仅更新一次
【发布时间】: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


【解决方案1】:

尝试在 MyWindow 类型的静态构造函数中执行 RegisterClassHandler,并提供静态方法。

你必须在回调方法中做一些不同的事情(因为它是静态的)才能找到你的 textBoxDescription 元素.....递归地使用 LogicalTree.GetParent 直到你找到你的 Window 元素......然后做一个 LogicalTree .FindLogicalNode 来查找你的 textBoxDescription 命名元素。

http://www.japf.fr/2009/08/wpf-memory-leak-with-eventmanager-registerclasshandler/

public partial class MyWindow : Window
{
    static MyWindow()
    {
        EventManager.RegisterClassHandler(typeof(FrameworkElement), FrameworkElement.MouseEnterEvent, new MouseEventHandler(MyMouseEventHandler));
    }

    public MyWindow()
    {
        InitializeComponent();
    }

    static 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();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2013-12-09
    相关资源
    最近更新 更多