1

首先,它是一个基于文档的 VSTO 项目(所以那些加载项 VSTO 演练并不真正起作用)。

我能够创建一个 ActionPaneControl 并能够使用 ElementHost 在其中添加一个 WPF 用户控件。启动它的代码如下:

ActionsPaneControl1 apc = new ActionsPaneControl1();            
Globals.ThisWorkbook.ActionsPane.Controls.Add(apc);
Globals.ThisWorkbook.ActionsPane.Visible = true;

但是,我正在尝试将参数传递到 WPF 用户控件中。然后我意识到在此代码中指示 WPF 用户控件的代码中没有任何位置。我的猜测是它与 ElementHost 有关。

有人可以帮忙吗?

谢谢

编辑:这是 ActionPaneControl1 类

partial class ActionsPaneControl1
{
 private System.ComponentModel.IContainer components = null;
 .....
 private void InitializeComponent()
    {
        this.elementHost1 = new 
        System.Windows.Forms.Integration.ElementHost();
        this.elementHost2 = new 
        System.Windows.Forms.Integration.ElementHost();
        this.ucWPF1 = new SWAPAEMonthlyReview.ucWPF();
      .....
     }
4

2 回答 2

1

感谢克里斯的回复,这激发了我的解决方案。这是代码。您可以将字符串 x 更改为您想要的任何内容。

在 WPF 中放这个

public ucWPF(string x)
    {
        InitializeComponent();
        //do whatever with x
    }

在 ActionPaneControl1.cs 中,把这个

partial class ActionsPaneControl1 : UserControl
{
    public ActionsPaneControl1(string x)
    {
        InitializeComponent(x);

    }
}

最后一步是在 ActionPaneControl1.Designer.cs 中编辑以下内容

public void InitializeComponent(string x)
    {
       ............
      this.ucWPF1 = new SWAPAEMonthlyReview.ucWPF(x);      
       .......
     }
于 2017-12-15T17:51:32.500 回答
0

您可以通过ElementHost.

public ActionsPaneControl1()
{
  InitializeComponent();
  if (elementHost1.Child != null)
  {
     var wpfControl = elementHost1.Child as WpfUserControlClassName;
  }
}
于 2017-12-15T14:17:14.630 回答