简体   繁体   中英

Textblock Text clears only on ShowDialog window is invoked WPF

I am trying to clear a TextBlock text which is inside a WPF window, before showing the Window as Dialog.

But the Text on TextBlock shows the Previous Value for a second, and then gets cleared automatically.

Is there any possibility I can clear text before showing window as Dialog?

Here is my code snippet:

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

Why are you calling Dispatcher.Invoke in the setter? Don't do this if you want to reset the text immediately before the ShowDialog method is called:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM