Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Binding values while or for loop

In my intuition, if I bind a value in the loop, I believed UI updates automatically.
This is my test code:

binding

private string _client1 {get; set;}
public string client1
{
    get => _client1;
    set
    {
        _client1 = value;
        RaisePropertyChanged(nameof(client1));
    }
}

Function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public async void UpdateUI()
{
    await Task.Run(() =>
    {
        DispatcherHelper.CheckBeginInvokeOnUI(async () =>
        {
            for (int i = 0; i < 10; i++)
            {
                _client1 = i.ToString();
                RaisePropertyChanged(_client1);

                await Task.Delay(200);
            }
        });
    }).ConfigureAwait(false);
}

Result
0 is shown and not changed in UI.

Expected Result
0 1 2 ... 10

I want the value to be changed continuously in UI. What is the problem with this code?

>Solution :

If the control is bound to the client1 property, then that is what you should be changing, not the backing field _client1:

client1 = i.ToString();

And since the setter for client1 already calls RaisePropertyChanged, you don’t need this line inside your loop:

RaisePropertyChanged(_client1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading