1

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();
      .....
     }
Heisenberg
  • 764
  • 6
  • 20

2 Answers2

1

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);      
       .......
     }
Heisenberg
  • 764
  • 6
  • 20
  • You shouldn't modify *.Designer.cs directly, there should be a comment right in it saying exactly that. It gets regenerated if you modify the UI with the designer. You don't need to pass it into `InitializeComponent` anyhow, you can access it as soon as it's in `ActionsPaneControl1.cs`. I think the root of the misunderstanding on my answer was that you didn't realize you could right click on the control and hit `View Code`. You should be doing all your custom code there. – Chris Dec 29 '17 at 17:39
0

You can access the WPF UserControl through the ElementHost.

public ActionsPaneControl1()
{
  InitializeComponent();
  if (elementHost1.Child != null)
  {
     var wpfControl = elementHost1.Child as WpfUserControlClassName;
  }
}
Chris
  • 3,400
  • 1
  • 27
  • 41
  • Thank you Chris. Please see my Edited post. Where do I put your code in? – Heisenberg Dec 15 '17 at 16:03
  • The constructor for `ActionsPaneControl1`. – Chris Dec 15 '17 at 16:19
  • uh....it is saying that ActionPaneControl1 already defines a member ProjectName called '.ctor' with the same parameter types...I think it has something to do with partial class? – Heisenberg Dec 15 '17 at 16:34
  • Take the code inside the constructor I posted and put it inside the constructor that already exists in your project. You should not have two constructors with the signature `public ActionsPaneControl1() {}`, only one. – Chris Dec 15 '17 at 16:38
  • Here is the odd thing, there is no other constructors called ActionsPaneControl1.....There is only a private void InitializeComponent(){} (I put part of it in my question). – Heisenberg Dec 15 '17 at 16:52
  • Found it. It was actually behind the designer.....kind of confusing when there was an error message. Thank you Chris! – Heisenberg Dec 15 '17 at 17:41