VSTO: Display WPF User Control in Custom Task Pane
Posted by Steve Pietrek on March 24, 2009
When attempting to add a WPF user control to using the following code, you will receive two compile time errors:
_salesDataTaskPane = CustomTaskPanes.Add(new SalesDataWPFUC(), "Sales Data");
_salesDataTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
_salesDataTaskPane.Visible = true;
_salesDataTaskPane.Width = 300;
- Error 1 The best overloaded method match for ‘Microsoft.Office.Tools.CustomTaskPaneCollection.Add(System.Windows.Forms.UserControl, string)’ has some invalid arguments C:\SharePoint_Test\SalesDataTaskPane\SalesDataTaskPane\ThisAddIn.cs
- Error 2 Argument ’1′: cannot convert from ‘SalesDataTaskPane.SalesDataWPFUC’ to ‘System.Windows.Forms.UserControl’ C:\SharePoint_Test\SalesDataTaskPane\SalesDataTaskPane\ThisAddIn.cs
To resolve this issue, you need to:
- Create a standard user control
- Create an ElementHost
- Create the WPF user control and assign to the ElementHost child
- Add the ElementHost to the standard user control Controls
- Dock the ElementHost to fill
- Add standard user control to CustomTaskPanes collection.
The code below shows a quick example of adding the WPF user control to CustomTaskPanes.
public partial class ThisAddIn {
private UserControl _salesDataUC;
private Microsoft.Office.Tools.CustomTaskPane _salesDataTaskPane;
private void ThisAddIn_Startup(object sender, EventArgs e) {
_salesDataUC = new UserControl();
ElementHost _eh = new ElementHost { Child = new SalesDataWPFUC() };
_salesDataUC.Controls.Add(_eh);
_eh.Dock = DockStyle.Fill;
_salesDataTaskPane = CustomTaskPanes.Add(_salesDataUC, "Sales Data");
_salesDataTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
_salesDataTaskPane.Visible = true;
_salesDataTaskPane.Width = 300;
}
...
Sorry, the comment form is closed at this time.

