Using User Control in Windows WPF (XAML) Application
User controls are an essential part of writing Windows/Web applications. The other day I was trying to get a WPF user control (XAML) added to my WPF Window and was running into issues.
I am working on RTM Windows Vista Ultimate with VS2005 SP1 (with patch for Vista), .net fwk 3.0 RTM and November 2006 CTP of WPF extensions for VS. The scenario is as below.
I created a new WPF based Windows application and in the project also added a WPF User Control. I then tried to use this user control in the main application window. For doing this, one needs to first add the required namespace directive as below, to the root Window tag.
The namespace qualifier can be anything that you want to use and I have used "my" here. This is then prefixed while using the user control. In the main XAML there will be statement like the following
Unfortunately, when you do this, the VS designer goes for a toss and doesn't renders the UI anymore. There is some issue in correctly identifying and working with the current assembly. The solution, however will build and run fine. Just that you loose the designer support.
I tried by moving the user control to a different project, adding reference to that project and then adding the assembly name the namespace tag, but that also didn't work. Though not a serious limitation, since the code does runs fine, but it will definitely hamper the page design since you can no longer view it.
I decided to give this a shot with the Orcas March 2007 CTP. Though there are a few issues in working with XAML, like automatic insertion of quotes for property values, automatic insertion of closing tag, when the opening tag is closed, doesn't work, but the user control issue is definitely resolved and the designer works properly. Adding the requisite namespace is also simpler due to intellisense support as shown in the figure below.
After this you can go ahead and add the necessary user control directive as something like below and all is well.
<my:UserControl1 Height="100" Width="100"></my:UserControl1>
Incidently, as soon as you type <my: intellisense will show the various controls available in this namespace for use and you will see UserControl1 also listed there.
BTW, the feature of not adding the closing tag automatically is actually good, since in earlier versions, if you are trying to add a container control tag, you need to yourself cut and the closing tag and paste where you need it. However with Orcas, you can add the opening tag, and then scroll down to where you want to insert the closing tag and type </ and intellisense will show the right options for closing tags.