How to Use WPF Controls Inside an Excel Actions Pa

2020-04-17 06:09发布

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

2条回答
萌系小妹纸
2楼-- · 2020-04-17 06:40

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

查看更多
爷的心禁止访问
3楼-- · 2020-04-17 06:48

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

查看更多
登录 后发表回答