Update – Apr 9, 2010
You do not need to do all this – instead you just need to set UpdateSourceTrigger on the Binding to PropertyChanged. I’ve made an updated blog entry on this here : Correction for the instant binding attached behavior for WPF TextBoxes
Original entry follows for posterity
The default behavior for a TextBox is to invoke data binding when the TextBox loses focus. Normally this is fine since people do have to hit an OK button somewhere, but there are times when you’d prefer that databinding occurs as you type into the TextBox. I wrote an attached property for the TextBox which invokes data binding as you type into it, and not when the text box goes out of focus. Here’s how you would use it.
<TextBox Height="23"
nsmvvm:TextInstantBindingBehavior.EnableInstantBinding="True"
Text="{Binding EnteredName}" HorizontalAlignment="Left"
Margin="144,64,0,0" Name="textBoxName"
VerticalAlignment="Top" Width="152" />
Here’s the code for the attached property.
public class TextInstantBindingBehavior
{
public static DependencyProperty EnableInstantBindingProperty =
DependencyProperty.RegisterAttached(
"EnableInstantBinding",
typeof(bool), typeof(TextInstantBindingBehavior),
new FrameworkPropertyMetadata(
new PropertyChangedCallback(EnableInstantBindingChanged)));
public static void SetEnableInstantBinding(TextBox target, bool value)
{
target.SetValue(
TextInstantBindingBehavior.EnableInstantBindingProperty, value);
}
public static bool GetEnableInstantBinding(TextBox target)
{
return (bool)target.GetValue(EnableInstantBindingProperty);
}
private static void EnableInstantBindingChanged(
DependencyObject target, DependencyPropertyChangedEventArgs e)
{
TextBox element = target as TextBox;
if (element != null)
{
if (e.NewValue != null && (bool)e.NewValue)
{
element.TextChanged += Element_TextChanged;
}
else
{
element.TextChanged -= Element_TextChanged;
}
}
}
static void Element_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = (TextBox)sender;
if (GetEnableInstantBinding(textBox))
{
BindingExpression bindingExpression =
textBox.GetBindingExpression(TextBox.TextProperty);
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
}
}
}
Read Full Post »