Using custom controls in WPF applications

December 6, 2007 at 10:43 am | Posted in Visual Studio 2008, WPF | 14 Comments

One thing that has frustrated me in both Silverlight and WPF applications is the use of custom controls–not their creation but how to add them to my project.  With all of the namespaces, assemblies, references that have to be used just right, my success rate has been about 50-50.  Every time I think I know the technique, I get floored by some mysterious error.  And God help you if you try to change the namespace of something!  I can easily get my solution in an unrecoverable state!

Google to the rescue?  Hardly. While there are many sites with examples of custom controls, they usually go into great detail about the creation but not how to successfully add it to your project.  This piece of magic is assumed to be obvious.  When the application is shown, the namespaces and assembly names are often the same as the project or other names in the solution so it was not so clear which name is the important reference.

So this post is my attempt to help anyone else who might have this problem by documenting the rules (that seem to work for me) for adding a custom (or user) control to your project.

Scenario 1: Add new User Control

This should be the easiest to do.

  1. 1. Project > Add User Control
  2. 2. In the dialog box now is the time to name the control.  In my example it will be LocalUserControl .  This will add the .xaml and .cs files for the control which can be used to create the control
  3. 3. In the .xaml file for the application that is going to use the control, add a reference to the namespace that is used by the UserControl like this:  xmlns:local=”clr-namespace:AddingControls”  Unless you have made namespace changes to the UserControl, this will be the same namespace as your application. 
  4. 4. Refer to the UserControl in your xaml file with the local tag: <local:LocalUserControl /> (The designer may not show the control immediately.  You may have to run the application one time.)
  5. 5. If you add an x:Name attribute to the control in the .xaml, you can reference it in the .cs file.

Scenario 2: Add existing User Control to your solution

Suppose you have an application with a local UserControl that you would like to use and possibly modify in a new application.  You want to add the source code of this control to your project.

  1. 1. Project > Add Existing Item
  2. 2. Browse to the solution folder that you are going to get the control from and select both the .xaml and .cs files for the control.  These should be added to your new solution.
  3. 3. In the .xaml file for the application that is going to use the control, add a reference to the namespace that is used by the UserControl like this:  <xmlns:local=”clr-namespace:AnotherUserControlProj”/>  Note that this will probably be different than the namespace for the application you are creating. 
  4. 4. Refer to the UserControl in your xaml file with the local tag: <local:AnExistingControl /> (The designer may not show the control immediately.  You may have to run the application one time.)
  5. 5. If you add an x:Name attribute to the control in the .xaml, you can reference it in the .cs file.

Scenario 3: Using an externally referenced control (.dll)

User controls may be supplied in .dll form.  This is how you will get third party controls, most likely.

  1. 1. Project > Add Reference…
  2. 2. Browse to the .dll to be used and add it.
  3. 3. In the .xaml file for the application that is going to use the control, add a reference to the namespace of the control and to its assembly like this: <xmlns:local3=”clr-namespace:Microsoft.Samples.CustomControls;assembly=ColorPicker” /> Both of these items will have to be provided in order to use the control.
  4. 4. Refer to the UserControl in your xaml file with the local tag: <local:ColorThumb />
  5. 5. If you add an x:Name attribute to the control in the .xaml, you can reference it in the .cs file.

How to add a WPF control library template to Visual C# Express 2008

December 5, 2007 at 9:05 am | Posted in C#, Visual Studio 2008, WPF | 7 Comments

Visual C# Express 2008 does not come with a project template for creating custom controls (.dll files).  You can create your own template by following these steps:

  1. 1. Open Visual C# Express 2008.
  2. 2. Create New WPF Application with name: MyLibrary
  3. 3. Project > Add User Control
  4. 4. In Solution Explorer, Delete Window1.xaml and App.xaml (the corresponding .cs files should be deleted by this step)
  5. 5. Project > MyLibrary Properties > Application > Output Type = Class Library
  6. 6. Build Solution.  There should be no errors and the bin/Release folder should have a MyLibrary.dll file
  7. 7. File > Export Template
  8. 8. Select Project Template then Next
  9. 9. Under Template Options change name to: WPF Control Library and Description: Create WPF Control .dll

The next time you open VS, you will find your newly created template ready to use.

Color Picker for WPF

December 3, 2007 at 8:06 am | Posted in C#, Visual Studio 2008, WPF | Leave a comment

I needed a color picker for WPF and found a simple design by Lee Brimelow here.

I used a bitmap image of a pallet from the Windows Forms ColorPicker dialog.

ColorPick

The problem is that it is not a complete color picker.  The missing variable is luminosity.

I found complete ColorPicker here.

Create a free website or blog at WordPress.com.
Entries and comments feeds.