Adding your WPF control to the toolbox

We all reuse our windows controls out of the toolbox so why not our WPF user controls? The process is really the same. To show the steps involved let’s first write a simple WPF user control.

 image

After renaming the control and adding a WPF application project to my solution(to test with), my solution looks like this

image

Since the functionality of the control is not important in this article, I just pasted in some animation code from msdn into my MyDemoControl.xaml file. 

<UserControl x:Class="MyDemoControl.MyDemoControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="156" Width="267">
    <!-- This example :
    from http://msdn.microsoft.com/en-us/library/system.windows.media.animation.pointanimation.aspx
    shows how to use PointAnimation to animate the
    position of an ellipse by animating the Center property of an 
    EllipseGeometry. PointAnimation is used because the Center property
    takes a Point value. -->
        <Canvas>
            <Path Fill="Blue" Margin="15,15,15,15">
                <Path.Data>
                    <!-- Describes an ellipse. -->
                    <EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
                    Center="0,0" RadiusX="15" RadiusY="15" />
                </Path.Data>
                <Path.Triggers>
                    <EventTrigger RoutedEvent="Path.Loaded">
                        <BeginStoryboard Name="MyBeginStoryboard">
                            <Storyboard>
 
                                <!-- Animate the Center property so that the ellipse animates from 
                                one point on the screen to another. -->
                                <PointAnimation
                                Storyboard.TargetProperty="Center"
                                Storyboard.TargetName="MyAnimatedEllipseGeometry"
                                Duration="0:0:2" From="0,0" To="156,267" RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Path.Triggers>
            </Path>
        </Canvas>
</UserControl>

Now let’s add the control to the toolbox!

Step 1:

 Add a using statement to the top of you control's .cs file. This is needed for the ToolboxBitmap we will be using.

using System.Drawing;

Step 2:

Add a image resource to the project for an icon. (right click the project in solution explorer, choose properties, choose Resources tab)

image

You can use an existing or create a new icon. I created a new one.

First select Images from the left dropdown

image

Then click the add resource button

image

Enter the image name in the dialog that pops up.

The name you use is extremely important.

image

Note it should be the class name followed by a period then the word “icon”.

image

I tried to draw a “W” for my icon.

In the properties window change the Build Action property for the BMP to Embedded Resource.

image

Note the full name of the file is the class + “icon” + the extension

Step 3:

Add an ToolboxBitmap attribute to your control class.

[ToolboxBitmap(typeof(MyDemoControl))]
public partial class MyDemoControl : UserControl
{
    public MyDemoControl()
    {
        InitializeComponent();

Notice only the type is needed because of our naming convention used on our bitmap.

Build the control project.

Step 4:

Now to add it to the toolbox,

Right click in the toolbox (where you want the control to be located) and select Choose Items.

 image

You may need to wait a bit for the Choose Toolbox Items dialog to appear. When it does,

Choose the WPF Components tab. This is important! The .Net tab will not work for WPF controls.

Click the browse button and navigate out to your DLL, choose it, click Open.

 image

Once you have selected your DLL you should see the icon that will be used for your control on the dialog 

image

 

Click OK. You should now see the control in your toolbox with proper icon.

image 

Step 5:

Switch back to the WPF Application project in the solution.

Drag your new control out of the toolbox and position it in the Window element.

Set the WPF Application project to be startup.

Run to test the behavior.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Wednesday, July 15, 2009 6:10 PM
Print

Comments on this entry:

# re: Adding your WPF control to the toolbox

Left by Ramaraju at 7/15/2009 5:46 PM
Gravatar
Nice blog. very helpful.

# re: Adding your WPF control to the toolbox

Left by Ramhisraja at 7/25/2009 10:11 AM
Gravatar
Yes, very nice bog.

# re: Adding your WPF control to the toolbox

Left by Frank at 9/16/2009 1:06 PM
Gravatar
I'm just trying to get the controls from the WPF panel of the choose toolbox item dialog to my toolbox. Why is it like hperdimensional physics? This software stinks to high heaven. I've checked the contols I want in the WPF panel and clicked OK but nothing shows up in my toolbox. What a bunch of crap!

# re: Adding your WPF control to the toolbox

Left by Wayne at 9/16/2009 11:00 PM
Gravatar
Frank, I agree the WPF integration in the IDE its not intuitive at all. This should get better with VS2010 and I would expect that it will be as seemless as windows development soon after. Double check that your image name is formatted with classname + "." + "icon". The naming is important.

# re: Adding your WPF control to the toolbox

Left by hair straighteners at 8/28/2010 5:06 AM
Gravatar
Wonderful blog! I truly love how it' s easy on my eyes as well as the data are well written. I am wondering how I can be notified whenever a new post has been made. I have subscribed to your rss feed which need to do the trick! Have a nice day!

# re: Adding your WPF control to the toolbox

Left by business icons at 11/22/2010 2:21 PM
Gravatar
Great stuff I agree the WPF integration in the IDE its not intuitive at all. This should get better with VS2010 and I would expect that it will be as seemless as windows development soon after. Double check that your image name is formatted with classname + "." + "icon". The naming is important.

# re: Adding your WPF control to the toolbox

Left by free icons at 11/29/2010 5:56 AM
Gravatar
Ive read your stuff before and youre just too awesome. I love what youve got here, love what youre saying and the way you say it. You make it entertaining and you still manage to keep it smart. I cant wait to read more from you. This is really a great blog.

# re: Adding your WPF control to the toolbox

Left by ramya at 1/12/2011 5:12 AM
Gravatar
How to add wpf control in toolbox of visual studio 2010 web application.

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910