0

I have a textbox which is bound to a property ItemID like so.

private string _itemID;
public string ItemID {
    get { return _itemID; }
    set { 
        _itemID = value;
    }
}

The XAML of the text box is as follows:

<TextBox Text="{Binding Path=ItemID, Mode=TwoWay}" Name="txtItemID" />

The problem is, the value of ItemID does not update immediately as I type,
causing the Add button to be disabled (command), until I lose focus of the text box by pressing the tab key.

Edwin
  • 850
  • 1
  • 11
  • 30

2 Answers2

5

Yes, by default, the property would be updated only on lost focus. This is to improve performance by avoiding the update of bound property on every keystroke. You should use UpdateSourceTrigger=PropertyChanged.

Try this:

<TextBox 
    Text="{Binding Path=ItemID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    Name="txtItemID" />

You should also implement the INotifyPropertyChanged interface for your ViewModel. Else, if the property is changed in the ViewModel, the UI would not get to know about it. Hence it would not be updated. This might help in implementation.

Shakti Prakash Singh
  • 2,356
  • 5
  • 32
  • 58
2

You need to fire the OnPropertyChanged event in the setter, otherwise the framework has no way of knowing that you edited the property.

Here are some examples:

Implementing NotifyPropertyChanged without magic strings

Notify PropertyChanged after object update

Community
  • 1
  • 1
Ilya Kogan
  • 21,366
  • 15
  • 81
  • 136
  • He is not facing a problem with property update. The property is getting updated properly after entering text in textbox. He is facing the issue that the property is not updating on each keystroke. What you are answering is regarding property binding with the control. This is already there. – Shakti Prakash Singh May 21 '13 at 08:24