Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Why does my UI only update after showing a MessageBox? This is my property:

C#
private string _abc;
public string abc
{
    get { return _abc; }
    set
    {
        _abc= value;
        base.OnPropertyChanged("abc");
    }
}

and XAML:

XML
<TextBlock Text="{Binding abc, UpdateSourceTrigger=PropertyChanged}" />


What I have tried:

If my code is like this the UI changes:

C#
abc= nrRowsImported.ToString();
MessageBox.Show(abc);

And if my code is like this, nothing changes:

C#
abc= nrRowsImported.ToString();

Why is this?

Bye.
Posted
Updated 16-Feb-22 9:58am
v2
Comments
Maciej Los 16-Feb-22 15:52pm    
Does a "abc" property is a part of some class? Please, provide more details.

Your application is running on a single thread, the thread that's created when your app starts. This thread also processes the application message pump. Windows sends your app various messages, like the mouse moving, windows being dragged around, buttons being clicked, windows that need repainting, ...

What is happening is your code is blocking this thread.

The update to the UI won't happen until your code returns, returning the thread back to an idle state. Since calling MessageBox.Show is a blocking call, the thread won't return to an idle state until the messagebox is dismissed.

When your code is not running, the app is free to process it's message pump and that's when your app will repaint its windows.
 
Share this answer
 
Take a look at MSDN documentation: How to: Implement Property Change Notification - WPF .NET Framework | Microsoft Docs[^]

Quote:
To support OneWay or TwoWay binding to enable your binding target properties to automatically reflect the dynamic changes of the binding source (for example, to have the preview pane updated automatically when the user edits a form), your class needs to provide the proper property changed notifications. This example shows how to create a class that implements INotifyPropertyChanged.


Here is an implementation: Explain INotifyPropertyChanged In WPF - MVVM[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900