0

The logic is two way bind Textbox's Text in the viewModel and to implement some function based on the Text change in the viewModel.
But when deleting all the text, there is a data binding error and the following functions depending on the Text change never begin. Text is bind to an int value. It looks like when binding failsimageQuantity = value, the following codes did not get called. Any ideas how to work around this?

public int ImageQuantity
{
    get { return imageQuantity; }
    set
    {
        imageQuantity = value;
        if (ImageQuantity > 0)
        {
            if (!String.IsNullOrEmpty(LastSymbol))
            {
                ImageAmount = ImageQuantity * (QQ.Ask + QQ.Bid) * 0.5;
            }
            OnPropertyChanged(() => this.ImageQuantity);
        }
        else
        {
            ImageAmount = 0.0;
        }  
    }

Edit:One way to fix this is to go back to implement TextChangedevent Command with prism.

baozi
  • 679
  • 10
  • 30

1 Answers1

1

Your ImageQuantity property setter will not be called when the user deletes all of the text in the data bound TextBox because your ImageQuantity property is an int and an empty string cannot be converted into an int. There is a simple solution to get around this.

If you need to call the code in your setter even if the input is invalid (such as an empty string), you could change your ImageQuantity property into a string and do your own conversion to int in the property setter (obviously with a try check):

public string ImageQuantity
{
    get { return imageQuantity; }
    set
    {
        int intValue = 0;
        if (int.TryParse(value, out intValue))
        {
            // value is a valid integer
            imageQuantity = value;
            if (!String.IsNullOrEmpty(LastSymbol))
            {
                ImageAmount = ImageQuantity * (QQ.Ask + QQ.Bid) * 0.5;
            }
            OnPropertyChanged(() => this.ImageQuantity);
        }
        else // value is a NOT a valid integer
        {
            ImageAmount = 0.0;
        }  
    }
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183