[英]Instantiate a Windows Forms control in WPF without default constructor
我正在嘗試在WPF中托管自定義Windows窗體控件。 我的自定義控件沒有公共構造函數,並且具有靜態的Create()
方法,該方法類似於以下內容:
public abstract class MyCustomControl : UserControl
{
internal MyCustomControl(...) { }
public static MyCustomControl Create(SomeEnum kind)
{
switch (kind)
{
case SomeEnum.Kind1:
return new MySuperCustomControl(...);
...
}
我想做的是在WPF中實例化此自定義控件,然后將其托管在WindowsFormsHost
,但是我顯然不能添加抽象類:
<wfi:WindowsFormsHost Width="250" Height="150">
<my:MyCustomControl x:Name="customControl" /> <-- doesn't work
</wfi:WindowsFormsHost>
我可以通過代碼將其添加到“主機”中嗎?
找到了它,它是WindowsFormsHost.Child
屬性。
如果沒有XAML中的公共構造函數,就無法托管控件。 您可以嘗試兩種方式:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.