Home > English > Databinding and Triggers – Part II

Databinding and Triggers – Part II

In our last post, we looked at creating a TextBoxBindingTrigger in order to fire updates on the TextChanged event instead of LostFocus. This time around we are going to be looking at another Trigger that will facilitate handling key press events. Let’s go ahead and take a look at the class that provides this functionality:

public class KeyTrigger : TriggerBase<FrameworkElement>
{
    private FrameworkElement _root;

    #region Key
    public Key Key
    {
        get { return (Key)GetValue(KeyProperty); }
        set { SetValue(KeyProperty, value); }
    }
    public static readonly DependencyProperty KeyProperty =
        DependencyProperty.Register("Key", typeof(Key),
        typeof(KeyTrigger), new PropertyMetadata(Key.None));
    #endregion

    #region Modifiers
    public ModifierKeys Modifiers
    {
        get { return (ModifierKeys)GetValue(ModifiersProperty); }
        set { SetValue(ModifiersProperty, value); }
    }
    public static readonly DependencyProperty ModifiersProperty =
        DependencyProperty.Register("Modifiers", typeof(ModifierKeys),
        typeof(KeyTrigger), new PropertyMetadata(ModifierKeys.None));
    #endregion

    private FrameworkElement FindRoot(FrameworkElement element)
    {
        FrameworkElement parent = element;
        while (parent != null)
        {
            element = parent;
            parent = element.Parent as FrameworkElement;
        }

        return element;
    }

    #region Overrides
    protected override void OnAttached()
    {
        base.OnAttached();

        _root = FindRoot(this.AssociatedObject);
        _root.KeyDown += HandleHostKeyDown;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        _root.KeyDown -= HandleHostKeyDown;
    }
    #endregion

    #region Events
    private void HandleHostKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == this.Key && Keyboard.Modifiers == this.Modifiers)
        {
            this.InvokeActions(e);
        }
    }
    #endregion

};

This class exposes two DependencyProperties: Key and Modifiers. The allow you to specify what key and modifier(s) you want to provide a response. The FindRoot method takes in the underlying FrameworkElement and attempts to walk up the VisualTree to find the root. The overrides OnAttahced() and OnDetaching() provide us with the ability to add an event handler to the KeyDown event. This in turn call the HandleHostKeyDown event handler with in turn fires our newly created trigger.

Let take a look and see how this is used in Xaml:

<i:Interaction.Triggers>
    <int:KeyTrigger Key="F5">
        <int:ExecuteDataMethod Method="RunQuery" />
    </int:KeyTrigger>
    <int:KeyTrigger Key="F5"  Modifiers="Control">
        <int:ExecuteDataMethod Method="ParseQuery" />
    </int:KeyTrigger>
</i:Interaction.Triggers>

Finally we can see that when an F5 or Ctl+F5 is pressed, we fire the RunQuery or ParseQuery methods correspondingly. As you have noticed, there is one more Trigger that is being used to allow us to call a given method on the DataContext or your ViewModel of your control. We will look at this Trigger in the following 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: