Well, it’s been a while since I’ve blogged, as I’ve been a busy little bee working on CodeStash. I feel guilty about this, so I’d like to take the chance to pass on the solution to a problem that was posted on CodeProject today. The scenario goes like this:
You have a ViewModel
with some text that you want to be updated, and you do all the usual binding work with some TextBox
elements. The thing is, you want to update your ViewModel
properties when the TextBox
loses focus. OK, so that’s easily achieved – in fact, it’s the default behaviour of the TextBox
. However, you have a button with IsDefault
set on it, so pressing Enter in the TextBox
triggers the button click – but as the TextBox
hasn’t lost focus, the property you are binding to doesn’t get updated. This is, of course, a problem. Fortunately, there’s an easy little trick that you can deploy to update the property, and it’s easily achieved using an attached behavior.
All you need to do is associate the behaviour to the Button
you have set the IsDefault
property on, and Bob is your mothers brother, the Text
property updates. So, what does this behavior look like:
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Data;
public class DefaultButtonUpdateTextBoxBindingBehavior : Behavior<Button>
{
protected override void OnAttached()
{
AssociatedObject.Click += AssociatedObject_Click;
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.Click -= AssociatedObject_Click;
}
void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
{
FrameworkElement el = Keyboard.FocusedElement as FrameworkElement;
if (el != null && el is TextBox)
{
BindingExpression expression = el.GetBindingExpression(TextBox.TextProperty);
if (expression != null)
{
expression.UpdateSource();
}
}
}
}
As you can see, there’s not that much code needed. Basically, we get the element that has focus, and retrieve the binding expression associated with it. Once you get the binding expression, we trigger the update.
I’ve copied this behaviour into CodeStash – the handy repository that I will be putting future snippets in for your delectation.