Progressbar for filling TextBox using MVVM Light Toolkit in WinRT

356 Views Asked by At

I have a form containing few TextBox elements and a progress bar. I want the progress bar to be updated when the TextBox's have some values assigned.

So when a value is set to a TextBox I increment ProgressPercent if the lenght is different from 0;

The problem is that I don't know what condition to use to check if any value was set before and to decrement if the TextBox will become blank again.

Bellow you have my code so far

ViewModel

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>

Any ideas how to do this?

2

There are 2 best solutions below

4
On BEST ANSWER

You should not notify property changes if the property did not change. You always can know for sure when it becomes empty and the other way around.

    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;
                }
            }
        }
    }
0
On

Track with a bool like this:

 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;
          }
         }
    }
}