Geeks With Blogs
Light Up the Web Blog about programming in Silverlight
Few days ago I wrote a small behavior, which I think, can be used in many applications, so I decided to share it with you :)

What are behaviors in Silverlight I won't write in this post, because there are many other places where you can find nice tutorial about this. I personally recommend this one: http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx

What I did

What I did is a "TargetedTriggerAction" behavior, which simulate button click on targeted button control, when user press "Enter" key in a TextBox control.

You probably think now that I did a useless code because you can do the same by handling KeyDown event in TextBox, but what about if you love MVVM, you want your code behind to stay clear and you don't want to make a mess in ViewModels? Then this solution is what you are looking for.

Where to use it?

The best place to use this behavior is a search controls, where user can finish operations by click Button control or pressing "Enter" key.

Live demo (pressing Enter key in the TextBox have the same result like clicking the button)

How to add it to your application

<Button x:Name="TargetedButton" Content="Targeted Button"  />
<TextBox Text="TextBox" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyDown" >
            <behavior:TextBoxEnterButtonInvoke TargetName="TargetedButton" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>
where "i:" is a namespace for "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" and "behavior:" is a namespace for behavior's code.

Source Code

Application is so small that I decided to enter all source code:

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Interactivity;

using System.Windows.Automation.Peers;

using System.Windows.Automation.Provider;

using System.Windows.Controls.Primitives;

 

/// TargetedTriggerAction for TextBox control.

/// It invokes targeted button when Enter is pressed inside TextBox.

/// <author>

/// Jacek Ciereszko

/// http://geekswithblogs.net/SilverBlog/

/// </author>

///

 

namespace TextBoxEnterBehavior

{

    public class TextBoxEnterButtonInvoke : TargetedTriggerAction<ButtonBase>

    {

        /// <summary>

        /// Gets or sets the peer.

        /// </summary>

        /// <value>The peer.</value>

        private AutomationPeer _peer { get; set; }

 

        /// <summary>

        /// Gets or sets the target button

        /// </summary>

        private ButtonBase _targetedButton { get; set; }

 

        /// <summary>

        /// Called after the TargetedTriggerAction is attached to an AssociatedObject.

        /// </summary>

        /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>

        protected override void OnAttached()

        {

            base.OnAttached();

            _targetedButton = this.Target;

            if (null == _targetedButton)

            {

                return;

            }

 

            // set peer

            this._peer = FrameworkElementAutomationPeer.FromElement(_targetedButton);

            if (this._peer == null)

            {

                this._peer = FrameworkElementAutomationPeer.CreatePeerForElement(_targetedButton);

            }

        }

 

        /// <summary>

        /// Called after targeted Button change.

        /// </summary>

        /// <remarks>Override this to hook up functionality to the new targeted Button.</remarks>

        protected override void OnTargetChanged(ButtonBase oldTarget, ButtonBase newTarget)

        {

            base.OnTargetChanged(oldTarget, newTarget);

            _targetedButton = newTarget;

            if (null == _targetedButton)

            {

                return;

            }

 

            // set peer

            this._peer = FrameworkElementAutomationPeer.FromElement(_targetedButton);

            if (this._peer == null)

            {

                this._peer = FrameworkElementAutomationPeer.CreatePeerForElement(_targetedButton);

            }

        }

 

        /// <summary>

        /// Invokes the targeted Button when Enter key is pressed inside TextBox.

        /// </summary>

        /// <param name="parameter">KeyEventArgs with Enter key</param>

        protected override void Invoke(object parameter)

        {

            KeyEventArgs keyEventArgs = parameter as KeyEventArgs;

            if (null != keyEventArgs && keyEventArgs.Key == Key.Enter)

            {

                if (null != _peer)

                {

                    IInvokeProvider invokeProvider = _peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;

                    invokeProvider.Invoke();

                }

            }

        }

    }

}

 


Download

Source code and live example is also available on
http://gallery.expression.microsoft.com/en-us/TextBoxInvokeButton


-----
Jacek Ciereszko

Polish version: http://jacekciereszko.pl/2009/09/behaviors-textbox-enter-button-invoke.html
Posted on Monday, September 21, 2009 4:57 PM | Back to top

Copyright © Jacek | Powered by: GeeksWithBlogs.net | Join free