Simple WPF Tool Window Sample

This small package demonstrates how to create a simple WPF tool window with VSXtra. The package has the same functionality as the tool window package created with the VSPackage wizard, but it uses WPF in the background. Thanks to Shawn Hempel for the sample and creating the WpfToolWindowPane!

Points of Interest:

  • Everything uses declarative approach
  • Command handling requires only a simple method decorated with attributes.
  • CommandExecMethod signs that the method is to be executed as a response for a command.
  • The CommandId attribute defines how the command is bound to the method.
  • The command has a ShowToolWindowAction attribute defines a menu action declaratively.
  • Creating a new tool window pane is easy with the ToolWindowPane<TPackage, TUIControl> base class.
  • InitialCaption and BitmapResourceId attributes set up the tool window properties declaratively.
  • You can refer to a resource string in the InitialCaption attribute.

Package source code:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra.Commands;
using VSXtra.Package;

namespace DeepDiver.WPFSimpleToolWindow
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "WPFSimpleToolWindow", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [XtraProvideToolWindow(typeof(MyWPFToolWindow))]
  [Guid(GuidList.guidWPFSimpleToolWindowPkgString)]
  public sealed class WPFSimpleToolWindowPackage : PackageBase
  {
    [CommandExecMethod]
    [CommandId(GuidList.guidWPFSimpleToolWindowCmdSetString, CmdIDs.cmdidShowWPFWindow)]
    [ShowToolWindowAction(typeof(MyWPFToolWindow))]
    private void ShowToolWindow()
    {
    }
  }
}
Tool window pane source code:

using System.Runtime.InteropServices;
using VSXtra.Windows;

namespace DeepDiver.WPFSimpleToolWindow
{
  [Guid("e6be3132-5e44-47cf-8ab8-7ff70e3e5d5d")]
  [InitialCaption("$ToolWindowTitle")]
  [BitmapResourceId(301)]
  public class MyWPFToolWindow : WpfToolWindowPane<WPFSimpleToolWindowPackage, MyControl>
  {
  }
}
Tool window WPF UI definition:

<UserControl x:Class="DeepDiver.WPFSimpleToolWindow.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
      <Button Name="button1" Padding="16" VerticalAlignment="Center" HorizontalAlignment="Center" 
        Click="button1_Click" BorderThickness="0" Background="BlanchedAlmond">
        _Click Me!
      </Button>
    </Grid>
</UserControl>
Tool window WPF UI source code:

using System.Globalization;
using System.Windows;
using System.Windows.Controls;

namespace DeepDiver.WPFSimpleToolWindow
{
  public partial class MyControl : UserControl
  {
    public MyControl()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show(string.Format(CultureInfo.CurrentUICulture, "We are inside {0}.button1_Click()", ToString()), "VSXtra WPF Simple Tool Window");
    }
  }
}
Screenshots:

WpfToolWindow.png

Last edited Feb 7, 2009 at 8:25 AM by INovak, version 3

Comments

No comments yet.