11

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.

  1. AcceptButton behaviour (Enter key triggers button press)
  2. AcceptButton formatting - i.e. the winforms button has the alternate formatting for accept buttons applied.

Many thanks, Chris

Chris
  • 1,201
  • 1
  • 12
  • 31
  • For point 2. It is possible to call NotifyDefault(true) on you windows forms button, it will show alternate formatting as if it was a default button. – ghord Dec 25 '20 at 10:31

6 Answers6

28

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"/>
TrialAndError
  • 1,647
  • 2
  • 20
  • 36
  • I don't know how an answer that does not address the questino at all is the most upvoted one. The question was about Windows Forms interop, not how to set use IsDefault/IsCancel in WPF. – ghord Dec 25 '20 at 10:26
3

Set your default button's IsDefault property to true.

0

WPF Windows 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).

user7116
  • 60,025
  • 16
  • 134
  • 166
  • I thought/feared this was the case. Going to leave unanswered for a few more days to see if any better ideas come up. Cheers – Chris Mar 27 '11 at 21:32
0

For using Esc and Enter I used this:

<UserControl.InputBindings>
    <KeyBinding Key="Esc" Command="{Binding CancelCommand}"/>
    <KeyBinding Key="Enter" Command="{Binding SaveCommand}"/>
</UserControl.InputBindings>
Jacob
  • 496
  • 5
  • 13
-1

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. :)

raddevus
  • 5,847
  • 5
  • 51
  • 65
-1

I'm not sure, but can you set the focus to that button? So it would have the same behavior as in winforms.

Aykut Çevik
  • 2,030
  • 3
  • 19
  • 27
  • 1
    The "accept button" doesn't have focus (normally). It is "clicked" when the user presses the Enter key in some other control (which has focus), if that control is configured not to accept Enter keypresses. It is a function of IsDialogMessage. – Tergiver Mar 25 '11 at 13:26
  • I need a textbox to have focus - the use case is a search field, and pressing enter should trigger the search. I'd rather not have to implement keyboard handling per field in winforms to support wpf. – Chris Mar 25 '11 at 13:54