Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a user control to a wpf window

I have a user control that I've created, however when I go to add it to the XAML in the window, Intellisense doesn't pick it up, and I can't figure out how to add it to the window.

like image 497
WedTM Avatar asked Jul 07 '09 16:07

WedTM


People also ask

How do I add user control to XAML?

The XamlFileBrowser Control Create a XAML application using Visual Studio 2008 and add a new item by right clicking on the project, select Add >> New Item from the menu and select User Control from the available templates.

How do you call a user control in WPF?

<Grid. ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>

What are user controls in WPF?

User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.

How do I add WPF control to Windows form?

Add a WPF control to a Windows FormOpen Form1 in the Windows Forms Designer. In the Toolbox, find the tab labeled WPFUserControlLibrary WPF User Controls. Drag an instance of UserControl1 onto the form. An ElementHost control is created automatically on the form to host the WPF control.


2 Answers

You need to add a reference inside the window tag. Something like:

xmlns:controls="clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName" 

(When you add xmlns:controls=" intellisense should kick in to make this bit easier)

Then you can add the control with:

<controls:CustomControlClassName ..... /> 
like image 160
Martin Harris Avatar answered Sep 23 '22 23:09

Martin Harris


You probably need to add the namespace:

<Window x:Class="UserControlTest.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="clr-namespace:UserControlTest"     Title="User Control Test" Height="300" Width="300">     <local:UserControl1 /> </Window> 
like image 30
user7116 Avatar answered Sep 23 '22 23:09

user7116