仅在ShowDialog窗口上调用Textblock Text才会调用WPF

时间:2017-07-19 08:01:07

标签: c# wpf modal-dialog window

我想在将窗口显示为对话框之前清除WPF窗口内的TextBlock文本。

但TextBlock上的Text显示一秒的Previous Value,然后自动清除。

在将窗口显示为Dialog之前,是否有可能清除文本?

这是我的代码段:

//Code in Window Control:
public string PopupTitle
{
    get
    {
        string response = string.Empty;
        this.Dispatcher.Invoke((Action)delegate
        {
            response = lbl_PopupTitle.Text;
        }, null);
        return response;
    }
    set
    {
        this.Dispatcher.Invoke((Action)delegate
        {
            lbl_PopupTitle.Text = value;
            lbl_PopupTitle.Visibility = string.IsNullOrEmpty(value) ? Visibility.Collapsed : Visibility.Visible;
        }, null);
    }
}

//Code to call this window:

PopupWindow popup = new PopupWindow();
popup.PopupTitle = string.Empty;
popup.ShowDialog();

1 个答案:

答案 0 :(得分:1)

为什么要在二传手中调用Dispatcher.Invoke?如果要在调用ShowDialog方法之前立即重置文本,请不要这样做:

set
{
    lbl_PopupTitle.Text = value;
    lbl_PopupTitle.Visibility = string.IsNullOrEmpty(value) ? Visibility.Collapsed : Visibility.Visible;
}