仅在 WPF 调用"显示对话框"窗口上清除的文本块文本



我正在尝试清除WPF窗口内的TextBlock文本,然后再将窗口显示为对话框。

但是 TextBlock 上的文本会显示上一秒的"上一个值",然后自动清除。

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

这是我的代码片段:

//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();

你为什么要在二传手中称呼Dispatcher.Invoke?如果要在调用ShowDialog方法之前立即重置文本,请不要执行此操作:

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

最新更新