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
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?
CodePudding user response:
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);