In WPF, you can assign a label a mnemonic, and tell it which control to activate with the “Target” property.
This doesn’t work if the target is a WindowsFormsHost. Is there a known solution to this?
Here’s an example. I am trying to make ALT-S activate the masked text box.
<Label
Width="Auto"
Target="{Binding ElementName=tbStartTime}"
TabIndex="12">
_Start Time:
</Label>
<WindowsFormsHost
Name="tbStartTime"
TabIndex="13">
<wf:MaskedTextBox Name="wfStartTime" Mask="90:00" />
/WindowsFormsHost>
I don’t think it’s possible, at least not without some extra boilerplate code… WPF and Windows Forms have a completely different model, and the
Targetproperty isn’t designed to refer to WinForms controls.Instead, I think you should use a WPF implementation of
MaskedTextBox, like this one (you can find many other examples with Google). Using WinForms controls in a WPF app is rarely a good idea if you can avoid it…EDIT: I just checked the doc : it is definitely not possible to do what you want, because the type of the
Label.Targetproperty isUIElement, and WinForms controls are clearly notUIElements…UPDATE: OK, I misread your code… you’re referencing the
WindowsFormsHost, which is aUIElement. Whoever voted me up was wrong too 😉I think the problem is that the
WindowsFormsHosttakes the focus when you press Alt-S, not theMaskedTextBox. Here’s a quick workaround :XAML :
Code-behind :
Anyway, my previous advice is still relevant : you’d better use a WPF MaskedTextBox…