Home > English > Databinding and Triggers – Part I

Databinding and Triggers – Part I

As I develop Silverlight applications, one of my main goals is to maintain a high user experience. I want the application to be simple, intuitive, and very responsive. With that said, I ran into a problem recently when I had published a new feature. I was trying to enhance the user’s experience by providing hot keys for common scenarios while out of the browser. The enhancement was on a screen that allowed users to enter in a query. The two key presses provided an alternative of needing to use the mouse. When a user clicked on the Play (F5) button, the screen would submit the query to the service and the server would return a resultset. When the user clicked on the Parse (Ctl+F5) button, the screen would submit the query to the service and server would return whether or not the query was valid.

Now comes the fun part. The default behavior for updating the binding on TextBox controls is on the LostFocus event. When a user clicks on a button this works perfectly fine but if a user presses the hot key(s), the control never loses focus and therefore the actual query is never pushed onto the ViewModel. Here is where Triggers come to the rescue.

The following code is a sample of a Trigger that will allow us to fire the UpdateSource method on the binding:

public class TextBoxBindingTrigger : TriggerAction<TextBox>
{
    protected override void Invoke(object parameter)
    {
        BindingExpression binding = 
            AssociatedObject.GetBindingExpression(TextBox.TextProperty);
        if (binding != null)
        {
            binding.UpdateSource();
        }
    }
};

As you can see, there really isn’t a lot of logic required to allow us to call the UpdateSource() method on the underlying binding. Obviously, in order for this to work, we do need to have a binding defined on the TextBox control.

Here is the Xaml that uses this newly created trigger:

<TextBox Grid.Row="2" TextWrapping="Wrap" 
    Text="{Binding QueryManagerQuery, Mode=TwoWay}" 
    AcceptsReturn="True">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="TextChanged">
            <int:TextBoxBindingTrigger />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

This now allows us to markup certain TextBox controls to fire updates on TextChanged instead of on LostFocus.

The final piece to this puzzle is how to respond to a key press and call a method on the ViewModel to respond just like a Button click. We will cover this on next blog post.

Advertisements
Categories: English Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: