当Value超过最小值或最大值时,WP7 ProgressBar会中断

时间:2012-02-19 20:52:43

标签: windows-phone-7 progress-bar

当Value超过最小值或最大值时,WP7 ProgressBar的视觉方面停止工作。具体来说,当超过最小值或最大值,然后值后来返回到Min和Max之间的值时,ProgressBar将完全陷入困境或完全填充(分别为Min和Max)。

这是我的测试代码,如果你们有兴趣再现这个。我该如何解决这个问题?

顺便说一句,我应该补充一点,我的最小值和最大值有所不同,有时值自然会超过这些值。

XAML:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Button Click="Button_Click" Width="150" Height="100" VerticalAlignment="Top">Up</Button>
        <Button Click="Button_Click_1" Width="150" Height="100" VerticalAlignment="Top" HorizontalAlignment="Right">Down</Button>
        <ProgressBar x:Name="PBar" Value="{Binding Progress}" Maximum="{Binding Maximum}"></ProgressBar>
        <TextBlock Text="{Binding Progress}" VerticalAlignment="Bottom"/>
    </Grid>

C#:

using System.ComponentModel;

public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        this.LayoutRoot.DataContext = this;
        Maximum = 100;
    }

    private int _progress;
    public int Progress
    {
        get { return _progress; }
        set
        {
            _progress = value;
            NotifyPropertyChanged("Progress");
        }
    }

    private int _maximum;
    public int Maximum
    {
        get { return _maximum; }
        set
        {
            _maximum = value;
            NotifyPropertyChanged("Maximum");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Progress += 10;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Progress -= 10;
    }
}

1 个答案:

答案 0 :(得分:0)

根据我上面的评论,我将在这里回答我自己的问题。我最终得到的答案是:

“稍微修改我的DomainModel,以便我的XAML绑定到另一个属性,检查是否超出Min和Max,如果超过,则只返回Min或Max。”