Vlad Vlad -4 years ago 261
C# Question

UWP ProgressBar and Binding

I have very strange situation with updating

ProgressBar
.
Basically, I have
SemanticZoom
and in
ZoomedIn
mode I have
ListView
. Each
ListView
contains
ProgressBar
. And here's the most interesting.

Working (values set by me):

<ProgressBar Minimum="1488240000" Maximum="1488241000" Value="{Binding CurrentTime, Mode=OneWay}" />


Not working (values are binding):

<ProgressBar Minimum="{Binding Start, Mode=OneTime}" Maximum="{Binding Finish, Mode=OneTime}" Value="{Binding CurrentTime, Mode=OneWay}" />


The way it's not working like that:
Start
is binded, OK.
Finish
is binded, OK.
CurrentTime
is binded,
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentTime)))
trigged, but here:

public double CurrentTime
{
get
{
return currentTime;
//It's trigged only first time
//But should be triggered every time PropertyChanged triggered
}

set
{
currentTime = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentTime)));
//It's OK, it's trigged every time I update this property
}
}


So, there's definitely something up to getter. Any ideas?

By the way, when I play with position of properties of
ProgressBar
I have strange results. For example, if I set
Minimum
, then
Maximum
and then
Value
I have
ProgressBar
100%. If I set
Value
,
Maximum
and
Minimum
- it's OK.

Answer Source

When binding Minimum and Maximum values in Extensible Application Markup Language (XAML), declare the Binding for Maximum first. If the Binding for Maximum is declared after Minimum, the bound value for Maximum is ignored and the following situations can occur:

To avoid this behavior, declare the Binding for Maximum first in your Extensible Application Markup Language (XAML).

For more info, please see Note under Maximum property.

And while binding Value property, the Binding must be TwoWay. So you can change your code like the following:

<ProgressBar Maximum="{Binding Finish, Mode=OneTime}" Minimum="{Binding Start, Mode=OneTime}" Value="{Binding CurrentTime,Mode=TwoWay}" />

Then it should be able to work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download