Reputation: 1251
I have a winforms usercontrol with multiple buttons in a WPF Control.
My usercontrol was previously hosted in a windows form and I was able to so
this.ParentForm.AcceptButton = this.btnSearch;
I'm trying to establish how to do similar on the usercontrol now that it is in the WindowsFormHost. The ParentForm property is null.
There are two things I would ideally like to achieve.
Many thanks, Chris
Upvotes: 13
Views: 8362
Reputation: 667
For using Esc and Enter I used this:
<UserControl.InputBindings>
<KeyBinding Key="Esc" Command="{Binding CancelCommand}"/>
<KeyBinding Key="Enter" Command="{Binding SaveCommand}"/>
</UserControl.InputBindings>
Upvotes: 0
Reputation: 9087
It is unfortunate there is no AcceptButton in WPF -- and annoying.
I successfully implemented the functionality by handling the Form's KeyUp event. Here's the code:
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
TargetButton_Click(sender, null);
}
}
Seems to work fine. In my case I have textboxes on the WPF form and the user enters values and hits and it seems to work fine. There may be issues with some control which overrides (grabs the KeyUp event) before the form or something so your mileage may vary. :)
Upvotes: -1
Reputation: 1854
Set Button.IsCancel (Esc) or IsDefault (Enter) on the Buttons in the page.
Example:
<Button Content="Yes" Click="Yes_Button_Click" IsDefault="True"/>
<Button Content="No" Click="No_Button_Click" IsCancel="True"/>
Upvotes: 33
Reputation: 64148
WPF Window
s do not have the Windows Forms concept of an AcceptButton
. You likely will not be able to get the Enter key to automatically engage your btnSearch.Click
handler. Also, you will not be able to get the alternate style for the accept button as well.
You could potentially expose a method on your Windows Forms control which acts like you clicked the search button, and call that method from the WPF side when the Enter key is pressed. Otherwise, you'll find that interaction between Forms controls and WPF controls are lacking (WindowsFormsHost
was never intended to provide full fidelity access).
Upvotes: 0
Reputation: 2088
I'm not sure, but can you set the focus to that button? So it would have the same behavior as in winforms.
Upvotes: -1