我在WPF控件中包装Windows Form遗留控件,我正在为一些Windows窗体控件执行此操作。即:
// WinFormHost control, only AWrapper is shown here, but there
//are BWrapper, CWrapper etc
public class AWrapper : WindowsFormsHost
{
Child=new ALegacyWinFormControl();
}
//WPF control, similarly there are BControl, CControl etc
<UserControl x:Class="AControl">
<local:AWrapper />
</UserControl>
在我的ResourceDictionary
我为每个WPF控件定义了一个键
// each WPF control has a key
<ResourceDictionary>
<DataTemplate x:Key="AControlKey">
<AControl />
</DataTemplate>
<DataTemplate x:Key="BControlKey">
<BControl />
</DataTemplate>
</ResourceDictionary>
我想要消除一层重复 - 我必须为每个WindowsFormsHost
包装器定义每个WPF控件,这是多余的。无论如何,我可以取消使用UserControl
对象,并将DataTemplate
笔直连接到WindowsFormsHost
控件?即:我正在看这样的事情:
// This wouldn't work
<ResourceDictionary>
<DataTemplate x:Key="AControlKey">
<AWrapper/>
</DataTemplate>
<DataTemplate x:Key="BControlKey">
<BWrapper/>
</DataTemplate>
</ResourceDictionary>
但上述似乎并不奏效。无论如何要做上述工作?它有可能吗?
编辑:
答案 0 :(得分:1)
如果AWrapper
是WindowsFormsHost
:
<DataTemplate x:Key="AControlKey">
<local:AWrapper/>
</DataTemplate>
样本用法:
<ContentControl Content="." ContentTemplate="{StaticResource AControlKey}" />
不涉及UserControl
。
local
映射到定义AWrapper
类的命名空间:
xmlns:local="clr-namespace:WpfApplication1"