VSTO 将变量传递给 ActionPane 和 WPF 用户控件/ElementHost

VSTO passing variables to ActionPane and WPF usercontrol /ElementHost

首先,它是一个基于文档的 VSTO 项目(因此那些加载项 VSTO 演练实际上不起作用)。

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

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

但是,我试图将一个参数传递给 WPF 用户控件。然后我意识到代码中没有地方指示这段代码中的 WPF 用户控件。我的猜测是它与 ElementHost 有关。

有人可以帮忙吗?

谢谢

编辑: 这是 ActionPaneControl1 class

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();
      .....
     }

您可以通过ElementHost访问WPF UserControl。

public ActionsPaneControl1()
{
  InitializeComponent();
  if (elementHost1.Child != null)
  {
     var wpfControl = elementHost1.Child as WpfUserControlClassName;
  }
}

感谢 Chris 的回复,它启发了我的解决方案。 这是代码。您可以将字符串 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);      
       .......
     }