VSTO passing variables to ActionPane and WPF usercontrol /ElementHost

263 Views Asked by At

first of all, It is a document based VSTO project (so those add-in VSTO walkthroughs don't really work).

I was able to create an ActionPaneControl and was able to add a WPF usercontrol in it using ElementHost. Code to launch it as the following:

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

However, I am trying to pass a parameter into the WPF usercontrol. then I realize that there is no place in the code indicating the WPF usercontrol in this code. My guess is that it has something to do with the ElementHost.

Can anyone help please?

Thank you

EDIT: Here is the 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();
      .....
     }
2

There are 2 best solutions below

6
On BEST ANSWER

You can access the WPF UserControl through the ElementHost.

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

Thanks for Chris's response, which inspired the solution on my end. Here is the code. You can change string x into anything you want.

In the WPF put this

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

in the ActionPaneControl1.cs, put this

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

    }
}

and last step is to edit the following in the ActionPaneControl1.Designer.cs

public void InitializeComponent(string x)
    {
       ............
      this.ucWPF1 = new SWAPAEMonthlyReview.ucWPF(x);      
       .......
     }