Reputation: 5747
Let me start by saying that I am new to MVVM
, so please bare with be, if the question is unclear let me know and I will try to clarify.
I have correctly implemented a button
in XAML
with multibinding
, the code is as follows:
<Button x:Name="licenceFilterSet" Content="Search License" Command="{Binding licenseSearchCommand}" Click="licenceFilterButtonClick" HorizontalAlignment="Right" Width="94">
<Button.Resources>
<local:SearchFilterConverter x:Key="SearchFilterConverter" />
</Button.Resources>
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource SearchFilterConverter}">
<Binding Path="Text" ElementName="filterLicenseTextBox" />
<Binding Path="IsChecked" ElementName="regularExpressionCheckBox" />
</MultiBinding>
</Button.CommandParameter>
</Button>
(the value of a textbox and a checkbox is sent to be proceed).
Now I would like to add the exact same feature to the textBox, where the user can directly press ENTER
in the textBox
and perform the same operation.
I tried the following code, but it seems that each character is now sent to the command, but I only want the command to be sent when the user presses ENTER
. What should I change?
<TextBox Name="filterLicenseTextBox">
<TextBox.Resources>
<local:SearchFilterConverter x:Key="SearchFilterConverter" />
</TextBox.Resources>
<TextBox.Text>
<MultiBinding Converter="{StaticResource SearchFilterConverter}">
<Binding Path="Text" ElementName="filterLicenseTextBox" Mode="OneWay"/>
<Binding Path="IsChecked" ElementName="regularExpressionCheckBox" Mode="OneWay"/>
</MultiBinding>
</TextBox.Text>
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding licenseSearchCommand}">
</KeyBinding>
</TextBox.InputBindings>
</TextBox>
Upvotes: 0
Views: 1402
Reputation: 11763
The short answer would be:
use the IsDefault
on the button:
<Button IsDefault="true"
Click= "licenceFilterButtonClick"
Content="Search License" ... />
Now theEnter
key is bound to this button Click, and if you hit Enter
on any place in the window with the button, it'll work.
The (bit) long answer would be:
Have a look at this article, it explains the why (creating a default button), and you can use the cancel as well, once you understand how they work :)
Upvotes: 3