在 WinRT 中使用 MVVM Light Toolkit 填充文本框的进度条

Progressbar for filling TextBox using MVVM Light Toolkit in WinRT

我有一个包含几个 TextBox 元素和一个进度条的表单。我希望在 TextBox 分配了一些值时更新进度条。

因此,当一个值设置为 TextBox 时,如果长度不为 0,我会递增 ProgressPercent;

问题是我不知道使用什么条件来检查之前是否设置了任何值以及在 TextBox 再次变为空白时递减。

到目前为止你有我的代码

视图模型

private string firstName { get; set; }
private string progressPercent { get; set; }

public string FirstName
{
    get
    {
        return this.firstName;
    }
    set
    {
        this.firstName = value;
        this.RaisePropertyChanged(() => this.FirstName);

        var vm1 = (new ViewModelLocator()).MainViewModel;
        if (value.Length != 0)              //   Checks the string length 
        {
            vm1.ProgressPercent += 3;
        }
    }
}
public int ProgressPercent
{
    get
    {
        return this.progressPercent;
    }
    set
    {
        this.progressPercent = value;
        this.RaisePropertyChanged(() => this.ProgressPercent);
    }
}

XAML

<StackPanel>
    <ProgressBar x:Name="progressBar1" 
                 Value="{Binding ProgressPercent ,Mode=TwoWay}"  
                 HorizontalAlignment="Left" 
                 IsIndeterminate="False" 
                 Maximum="100"
                 Width="800"/>
    <TextBlock Text="First Name"/>
    <TextBox x:Name="FirstNameTextBox" Text="{Binding FirstName, Mode=TwoWay}"/>
</StackPanel>

知道怎么做吗?

用这样的 bool 进行跟踪:

 private bool firstNamePoints=false;
 public string FirstName
 {
    get
    {
        return this.firstName;
    }
    set
    {
        this.firstName = value;
        this.RaisePropertyChanged(() => this.FirstName);

        var vm1 = (new ViewModelLocator()).MainViewModel;
        if (value.Length != 0)              //   Checks the string length 
        {
          if(!firstNamePoints)
           {
            vm1.ProgressPercent += 3;
            firstNamePoints=true;
           }
        }
        else
        {
          if(firstNamePoints)
          {
            vm1.ProgressPercent -= 3;
            firstNamePoints=false;
          }
         }
    }
}

如果 属性 未更改,您不应通知 属性 更改。你总是可以确定它什么时候变空,什么时候变空。

    public string FirstName
    {
        get
        {
            return this.firstName;
        }
        set
        {
            if (this.firstName != value)
            {
                bool oldValueIsEmpty = String.IsNullOrWhiteSpace(this.firstName);
                this.firstName = value;
                this.RaisePropertyChanged(() => this.FirstName);

                var vm1 = (new ViewModelLocator()).MainViewModel;
                if (String.IsNullOrWhiteSpace(value))              //   Checks the string length 
                {
                    vm1.ProgressPercent -= 3;
                }
                else if (oldValueIsEmpty)
                {
                    vm1.ProgressPercent += 3;
                }
            }
        }
    }