Developing & deploying custom user controls in WPF

In WPF the development and deployment of custom user controls is similar to the development and deployment of user controls in ASP.NET. Let say that you have made a user control named MyUserCtrl1 in your main application project. In order to use this control in a Window you must first declare a namespace for it. The easy way to do this in the Window tag. After that the use of the control is as easy as writing a tag with the namespace that you have declared in the window tag and name – the name of the control:

XAML:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyApplication"
Title="Window1"
>
...
<uc:MyUserCtrl1 />
...
</Window>

Where MyApplication is the namespace of your application.
Now if the user control is in another assembly (for example MyApplication.GUI.dll) you must also type the assembly name in the XML namespace declaration:

XAML:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyApplication.GUI;assembly=MyApplication.GUI"
Title="Window1"
>
...
<uc:MyUserCtrl1 />
...
</Window>

Note that the assembly name may be different from the namespace!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.