UWP ProgressBar 和绑定
UWP ProgressBar and Binding
我在更新 ProgressBar
时遇到了非常奇怪的情况。
基本上,我有 SemanticZoom
,在 ZoomedIn
模式下我有 ListView
。每个 ListView
包含 ProgressBar
。这是最有趣的。
工作(我设置的值):
<ProgressBar Minimum="1488240000" Maximum="1488241000" Value="{Binding CurrentTime, Mode=OneWay}" />
无效(值具有约束力):
<ProgressBar Minimum="{Binding Start, Mode=OneTime}" Maximum="{Binding Finish, Mode=OneTime}" Value="{Binding CurrentTime, Mode=OneWay}" />
它不是那样工作的方式:
Start
已绑定,OK。 Finish
已绑定,OK。 CurrentTime
已绑定,PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentTime)))
已触发,但此处:
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
}
}
所以,肯定有 getter 的东西。有什么想法吗?
顺便说一下,当我使用 ProgressBar
属性的位置时,我得到了奇怪的结果。例如,如果我设置 Minimum
,然后 Maximum
然后 Value
我有 ProgressBar
100%。如果我设置 Value
、Maximum
和 Minimum
- 没问题。
我想这会解决问题
<ProgressBar
Minimum="{Binding Min, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Maximum="{Binding Max, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Value="{Binding Progress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ProgressBar>
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:
- If the bound Minimum value is negative, the Maximum value is set to 0.
- If the bound Minimum value is greater than the default Maximum (100 for Slider and ProgressBar), the Maximum value is set equal to the Minimum value.
To avoid this behavior, declare the Binding for Maximum first in your Extensible Application Markup Language (XAML).
有关详细信息,请参阅 Maximum 属性 下的 注意。
并且在绑定时 Value property, the Binding 必须是 TwoWay
。所以你可以像下面这样更改你的代码:
<ProgressBar Maximum="{Binding Finish, Mode=OneTime}" Minimum="{Binding Start, Mode=OneTime}" Value="{Binding CurrentTime,Mode=TwoWay}" />
那应该就可以了
我在更新 ProgressBar
时遇到了非常奇怪的情况。
基本上,我有 SemanticZoom
,在 ZoomedIn
模式下我有 ListView
。每个 ListView
包含 ProgressBar
。这是最有趣的。
工作(我设置的值):
<ProgressBar Minimum="1488240000" Maximum="1488241000" Value="{Binding CurrentTime, Mode=OneWay}" />
无效(值具有约束力):
<ProgressBar Minimum="{Binding Start, Mode=OneTime}" Maximum="{Binding Finish, Mode=OneTime}" Value="{Binding CurrentTime, Mode=OneWay}" />
它不是那样工作的方式:
Start
已绑定,OK。 Finish
已绑定,OK。 CurrentTime
已绑定,PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentTime)))
已触发,但此处:
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
}
}
所以,肯定有 getter 的东西。有什么想法吗?
顺便说一下,当我使用 ProgressBar
属性的位置时,我得到了奇怪的结果。例如,如果我设置 Minimum
,然后 Maximum
然后 Value
我有 ProgressBar
100%。如果我设置 Value
、Maximum
和 Minimum
- 没问题。
我想这会解决问题
<ProgressBar
Minimum="{Binding Min, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Maximum="{Binding Max, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Value="{Binding Progress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ProgressBar>
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:
- If the bound Minimum value is negative, the Maximum value is set to 0.
- If the bound Minimum value is greater than the default Maximum (100 for Slider and ProgressBar), the Maximum value is set equal to the Minimum value.
To avoid this behavior, declare the Binding for Maximum first in your Extensible Application Markup Language (XAML).
有关详细信息,请参阅 Maximum 属性 下的 注意。
并且在绑定时 Value property, the Binding 必须是 TwoWay
。所以你可以像下面这样更改你的代码:
<ProgressBar Maximum="{Binding Finish, Mode=OneTime}" Minimum="{Binding Start, Mode=OneTime}" Value="{Binding CurrentTime,Mode=TwoWay}" />
那应该就可以了