簡體   English   中英

在沒有默認構造函數的情況下在WPF中實例化Windows窗體控件

[英]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中的公共構造函數,就無法托管控件。 您可以嘗試兩種方式:

  1. 定義WindowsFormsHost的名稱,並將WindowsFormsHost的Child屬性從C#代碼中的靜態Create()設置為您的實例。 例如在initialize(或load)方法中。 -這很簡單。
  2. 嘗試將WindowsFormsHost的Child屬性綁定到Create()方法。 坦白說,我不知道這種方法是否行得通……但是您可以嘗試:) ..如何綁定到XAML中的方法? 您可以閱讀- 或嘗試查看msdn或google :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM