Wednesday, April 1, 2015

Solution for textBox text change not updating the view model/model

<TextBox Name="textBox1"
      Height="23" Width="463"
      HorizontalAlignment="Left" 
      Margin="12,12,0,0"   
      VerticalAlignment="Top"
      Text="{Binding OriginalText, UpdateSourceTrigger=PropertyChanged}" /> 
The TextBox.Text property has a default UpdateSourceTrigger value of LostFocus. This means if an application has a TextBox with a data-bound TextBox.Text property, the text you type into the TextBox does not update the source until the TextBox loses focus (for instance, when you click away from the TextBox).
If you want the source to get updated as you are typing, set the UpdateSourceTrigger of the binding to PropertyChanged. In the following example, the Text properties of both the TextBox and the TextBlock are bound to the same source property. The UpdateSourceTrigger property of the TextBox binding is set to PropertyChanged.

Serialization Exception: PropertyChangedEventManager is not serializable

Since events are non serializable, here is the way out to deal the problem when serializing model classes which have a property changed event to notify the view model classes:

http://www.primordialcode.com/blog/post/serialization-exception-propertychangedeventmanager-serializable

[NonSerialized]
private PropertyChangedEventHandler _PropertyChanged;
 
public virtual event PropertyChangedEventHandler PropertyChanged
{
  add { _PropertyChanged += value; }
  remove { _PropertyChanged -= value; }
}