Lots of time when a user is on a page or entering text in a textbox and presses the enter key, you want a default button to be invoked automatically. A common scenario would be on a search page. When a user presses the enter key in a textbox, you want the search button to be invoked automatically. You could handle the keydown event of each and every textbox or you could create a trigger that you just attach to the textbox.
I decided to create a trigger that can be attached to any control.
public class DefaultEnterButtonTrigger : 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 Control.
/// </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 && _peer.IsEnabled())
{
IInvokeProvider invokeProvider = _peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
invokeProvider.Invoke();
}
}
}
}
Now to use the trigger you need to add a reference to the System.Windows.Interactivity.dll
In your xaml filed add
xmlns
:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
and add your trigger
xmlns:triggers="clr-namespace:MyApp.Triggers;assembly=MyApp" //your trigger that you created above
Now add a textbox and a button to your xaml page
<TextBox x:Name="myTextBox >
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown" >
<triggors:DefaultEnterButtonBehavior TargetName="SearchButton" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<Button x:Name="SearchButton" Content="Search" Click="SearchButton_Click" />
Run your app and when your press the enter button from with in your textbox, your search button will be invoked.