.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Invoke a command with enter key after typing in a textbox
 

Invoke a command with enter key after typing in a textbox

Hi,

<Stackpanel>
<Button command={Binding SearchCommand} Content="Click to search">
<TextBox Text="{Binding SearchPhrase}">
</Stackpanel>

Is it possible to invoke SearchCommand with enter/return key, after having typed search phrase into the textbox ? Currently user has to move mouse over the search button and click it..


Thanks for help,
Michal

Michał Januszczyk
You can set an InputBinding to do this:

<TextBox Text="{Binding SearchPhrase}">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding SearchPhrase}" Key="Enter"/>
    </TextBox.InputBindings>            
</TextBox>


http://weblogs.asp.net/marianor/
Mariano O. Rodriguez
Hi Mariano, thanks for help.
Generally the concept is right, but there is one thing missing: Command property cannot be asigned-to with value {Binding ACommand}. Some kind of wrapper is required.

<UserControl.Resources>
<!-- this time Command property _is_ a dependency property
<mvvmUtils:CommandReference x:Key="searchCommandRef"Command={Binding ACommand} />

</UserControl.Resources>

...
...

<TextBox Text="{Binding SearchPhrase}">
<TextBox.InputBindings>
<KeyBinding Command="{StaticResource searchCommandRef}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>

A sample CommandReference class can be found here:
http://cid-890c06c8106550a0.skydrive.live.com/self.aspx/BlogSamples/JoyfulWPF/MvvmCommandRederence.zip

And The article that explains that all again:
http://joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html



Thanks again,
Michal
Michał Januszczyk
You can set an InputBinding to do this:

<TextBox Text="{Binding SearchPhrase}">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding SearchPhrase}" Key="Enter"/>
    </TextBox.InputBindings>            
</TextBox>


http://weblogs.asp.net/marianor/
Mariano O. Rodriguez
Hi Mariano, thanks for help.
Generally the concept is right, but there is one thing missing: Command property cannot be asigned-to with value {Binding ACommand}. Some kind of wrapper is required.

<UserControl.Resources>
<!-- this time Command property _is_ a dependency property
<mvvmUtils:CommandReference x:Key="searchCommandRef"Command={Binding ACommand} />

</UserControl.Resources>

...
...

<TextBox Text="{Binding SearchPhrase}">
<TextBox.InputBindings>
<KeyBinding Command="{StaticResource searchCommandRef}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>

A sample CommandReference class can be found here:
http://cid-890c06c8106550a0.skydrive.live.com/self.aspx/BlogSamples/JoyfulWPF/MvvmCommandRederence.zip

And The article that explains that all again:
http://joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html



Thanks again,
Michal
Michał Januszczyk

You can use google to search for other answers

Custom Search

More Threads

• convert an HTML string into a Flow Document
• opening external URL from XAML
• ResourceDictionary & Loose XAML
• Accessing controle from other class in project
• How can I use alphabetical order to show to properties in WPF?
• TreeView theme
• Error 4 The attribute 'Class' does not exist in XML namespace; bug?
• Skinning the ItemsPanel of a GridView
• double precision in transform?
• Is it technically possible to build an Office 12 Ribbon control in WPF v1?