WPF:使Target属性引用WindowsFormsHost中的控件

时间:2009-09-15 21:43:35

标签: wpf xaml

在WPF中,您可以为标签分配助记符,并告诉它使用“目标”属性激活哪个控件。

如果目标是WindowsFormsHost,则不起作用。有没有已知的解决方案?

这是一个例子。我试图让ALT-S激活蒙面文本框。

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

1 个答案:

答案 0 :(得分:2)

我认为这是不可能的,至少没有一些额外的样板代码...... WPF和Windows Forms有一个完全不同的模型,而Target属性并不是为了引用WinForms控件而设计的。

相反,我认为您应该使用MaskedTextBox的WPF实现,例如this one(您可以在Google上找到许多其他示例)。如果你可以避免使用WPF应用程序中的WinForms控件,那么这很有用......

编辑:我刚检查了文档:绝对不可能做你想要的,因为Label.Target属性的类型是UIElement,而WinForms控件显然不是{{1} }Š...


更新:好的,我误读了您的代码......您引用的是UIElement WindowsFormsHost。投票给我的人也是错的; - )

我认为问题是UIElement在按下Alt-S而不是WindowsFormsHost时会成为焦点。这是一个快速的解决方法:

XAML:

MaskedTextBox

代码隐藏:

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

无论如何,我之前的建议仍然相关:你最好使用WPF MaskedTextBox ......