3

There are several resources out there that explain how to add WinForms controls to Excel. See these two:

http://msdn.microsoft.com/en-us/library/vstudio/e3zbk0hz%28v=vs.100%29.aspx

http://www.clear-lines.com/blog/post/create-excel-2007-vsto-add-in-wpf-control.aspx

Both of them mention the more up-to-date option of using WPF controls (the one I need). Unfortunately, both resources are lacking the fundamental part. There is a missing link:

(1) The Microsoft site mentions some video -with the exact title that matches my requirement- but all videos in that web site have been removed, it seems.

enter image description here

(2) The Clear-Lines site contains an outstanding, step-by-step project but alas, when the critical part is mentioned, the author uses some facility (the "WPF Interoperability section of the Toolbox") that does not exist in VS-2010+

enter image description here

Based on the above screenshots, and other sites, I have come to the conclusion that the missing link, the connection between my WPF UserControl and its appearance in Excel is some ElementHost.

Addendum for @HighCore. See Toolbox below:

enter image description here

  • 1
    Being a WPF coder, I also tried to use WPF Controls in VSTO applications, but so far I have had to use the inferior WinForms approach because it is documented. – Travis Banger Jul 20 '14 at 14:45
  • 1
    VSTO applications work great with WPF. I am currently refactoring a Win Form app for Excel that used a browser to get custom HTML from a web service into a WPF application. The most important thing for me was learning how to manipulate the WPF to appear on the screen while NOT using a WPF application. To overcome this I started here: http://www.c-sharpcorner.com/UploadFile/yougerthen/integrate-wpf-control-in-your-excel-solutions/ which led to me the current architecture. A WPF library of Controls and Windows and a Excel plugin project. Referenced the other app and everything is good to go. – Anthony Mason Oct 25 '14 at 22:14

2 Answers2

2

Esteemed Self:

Your problem is that you are trying to place a WPF Control inside another WPF Control.

You need to create an old-fashioned WinForm Control and next you use the Toolbox as depicted here:

enter image description here

Notice that the section ElementHost Tasks has been renamed WPF Interoperability but it is otherwise very much alive.

From MSDN Magazine:

http://msdn.microsoft.com/en-us/magazine/cc163292.aspx#S4

enter image description here

0

I'm not sure why you think there's a "missing link". The ElementHost is the standard way to host WPF content on winforms, and it's perfectly documented in the link above, and also here.

given any winforms container (such as a Form or Panel), simply do:

var elementHost = new ElementHost 
                  {
                      Child = new YourWPFContentHere()
                  };

this.Controls.Add(elementHost);

make sure you add references to these assemblies:

  • PresentationCore

  • PresentationFramework

  • System.Xaml

  • WindowsBase

  • WindowsFormsIntegration

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • 2
    I guess I was looking for a pure WPF solution, then. BTW: Both the video and the "WPF Interoperability section of the Toolbox" are definitely missing. –  Jul 20 '14 at 15:01
  • @swiss_programmer I'm not familiar with the "toolbox" (whatever that is), but the above code should work. – Federico Berasategui Jul 20 '14 at 15:03
  • 1
    Toolbox = Ctrl-Alt-X in Visual Studio. –  Jul 20 '14 at 15:06
  • 1
    HighCore: do you have experience with VSTO? Is is really hard to find good info (such as the one subject of this post) about it. –  Jul 20 '14 at 15:07
  • @swiss_programmer I was being sarcastic dude, you don't need any "toolbox" in WPF because there's XAML you can write. No, unfortunately I don't have any experience with VSTO, but if you can host winforms content, you surely can host WPF content via the ElementHost. – Federico Berasategui Jul 20 '14 at 15:14
  • 1
    Okay - You remind me of a colleague who typed HTML the way a novelist types English, at high speed. That was before the introduction of visual HTML tools. I am slowly learning my way to XAML, after being intimidated by it. Will try your suggestion. –  Jul 20 '14 at 15:21
  • @swiss_programmers both HTML and XAML, being markup languages, lend themselves to use manual typing rather than visual designers. for XAML in particular, the Visual Studio designer generates a lot of unwanted, unmaintainable attributes for XAML tags. See [My explanation](http://stackoverflow.com/a/18927377/643085) of why the use of the Visual Studio designer is discouraged in WPF, and why creating your own XAML is the preferred way. – Federico Berasategui Jul 20 '14 at 15:26
  • 1
    "the Visual Studio designer generates a lot of unwanted, unmaintainable attributes for XAML tags." @HighCore: Just like computers watch over our shoulders and prevent us from making mistakes, it is our duty to reciprocate and watch over the best-effort code generated by Visual Studio. Such code is bound to become better and better (unlike human intelligence :-( –  Jul 20 '14 at 20:49