WPF: Create Target property specific to control in WindowsFormsHost

In WPF, you can assign a mnemonic to a label and tell it which control to activate using the Target property.

This does not work if the target is WindowsFormsHost. Is this solution known?

Here's an example. I am trying to get ALT-S to activate a hidden textbox.

<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>

      

+2


source to share


1 answer


I don't think this is possible, at least not without some additional templating code ... WPF and Windows Forms have a completely different model, and the property is Target

not meant to refer to WinForms controls.

Instead, I think you should use a WPF implementation MaskedTextBox

like this one (you can find many other examples from Google). Using WinForms controls in a WPF application is rarely a good idea if you can avoid it ...

EDIT: I just checked the doc: you can't do what you want because the type of the property Label.Target

UIElement

and the WinForms controls are clearly not UIElement

s ...


UPDATE: Ok I am reading your code wrong ... you are linking to WindowsFormsHost

which is UIElement

. Whoever voted for me was also wrong; -)

I think the problem is that it WindowsFormsHost

focuses when you press Alt-S and not MaskedTextBox

. Here's a quick way:



XAML:

<WindowsFormsHost 
    Name="tbStartTime" 
    TabIndex="13"
    GotFocus="tbStartTime_GotFocus">
 <wf:MaskedTextBox Name="wfStartTime"  Mask="90:00" />
</WindowsFormsHost>

      

Code:

    private void tbStartTime_GotFocus(object sender, RoutedEventArgs e)
    {
        tbStartTime.Child.Focus();
    }

      

Anyway, my previous advice still matters: it's better to use WPF MaskedTextBox ...

+2


source







All Articles