Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I Have a text box, depending on the text box value i need to enable/disable other text Boxes.I am using MVVM Pattern.

So here's my problem , whenever i enter some text in TextBox1 ,the Setter for TextBox1 is getting fired and i am able to check in if loop ,whether valus exists and i am disabling other text boxes. Now, when there is single value in text box say "9" and i am deleting / Backspacing it the Set event is not getting triggered in order to enable the other Text Boxes.

View:

<TextBox Text = {Binding TextBox1 , UpdateSourceTrigger = PropertyChanged,Mode= TwoWay}/>
<TextBox Text = {Binding TextBox2 , UpdateSourceTrigger = PropertyChanged,Mode= TwoWay}/>
<TextBox Text = {Binding TextBox3 , UpdateSourceTrigger = PropertyChanged,Mode= TwoWay}/>

View Model:

private int_textBox1;
public int TextBox1
{
 get {return _textBox1;}
 set 
   {
     _textBox1= value;
     if(value > 0)
       {
          //Code for Disabling Other Text Boxes (TextBox2 and TextBox3)
       }
      else
       {
          // Code for Enabling Other Text Boxes (TextBox2 and TextBox3)
       }
     NotifyPropertyChanged("TextBox1");
   }
}
share|improve this question
1  
To tell the truth it must give compiler error, because value is String and you are comparing it with Int value like 0. In my opinion you must write value.Length instead of value. – Farhad Jabiyev Jun 5 '13 at 6:51

2 Answers 2

If you are using MVVM pattern, you should create boolean properties, and bind TextBox.IsEnabled property to it. Your boolean properties should raise PropertyChanged event, in order to tell the view (TextBox in your case) that your property was really changed:

public bool IsEnabled1
{
    get { return _isEnabled1; }

    set
    {
        if (_isEnabled1 == value)
        {
            return;
        }

        _isEnabled1 = value;
        RaisePropertyChanged("IsEnabled1");
    }
}

and then in xaml:

<TextBox Text="{Binding TextBox1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
         IsEnabled="{Binding IsEnabled1}" />

and so on with other TextBoxes

share|improve this answer

first of all, if you set your updatesourcetrigger to Propertychanged - your setter is called when you do anything in your textbox. i check this in a simple testproject. btw do you call OnPropertyChanged in your setter, because its not in your sample code?

if not your binding seems to be broken. so pls check your binding or post some more relevant code.

EDIT:

if you change your type to int? you can do the following xaml

    <TextBox Text="{Binding MyNullableInt, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, TargetNullValue=''}"/>
share|improve this answer
    
But my Setter is not firing when i delete the single value in the text box?? so , how can i achieve this firing, even if they delete single value, in order to disable other text boxes?? – Ujjwal27 Jun 5 '13 at 8:19
    
i dont understand why this happen in your project. my test application setter is called no matter if i use DEL or backspace or what ever. can you pls check your bindings at runtime with Snoop? – blindmeis Jun 5 '13 at 9:03
    
Could you please try changing the String to Int for TextBox1 Pls – Ujjwal27 Jun 5 '13 at 10:50
1  
int property can not work. and yes then your setter is not called, even a converter would not be called. if you want such a behavior use a string property in your viewmodel and convert the value to your int property in your model if needed – blindmeis Jun 5 '13 at 12:02
1  
check your output window you simply get a bindingerror exception because wpf cannot convert null to int. but you can use Nullable<int> if you want – blindmeis Jun 6 '13 at 5:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.