Everything SharePoint and XAML

My continuous learning of SharePoint, XAML, Silverlight, Windows Phone 7, Windows 8, Office, VSTO, C#…

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:

  1. Create a standard user control
  2. Create an ElementHost
  3. Create the WPF user control and assign to the ElementHost child
  4. Add the ElementHost to the standard user control Controls
  5. Dock the ElementHost to fill
  6. 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() };
_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.

%d bloggers like this: