74

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
WedTM
  • 2,587
  • 5
  • 37
  • 54

4 Answers4

88

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 ..... />
Martin Harris
  • 28,277
  • 7
  • 90
  • 101
14

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>
user7116
  • 63,008
  • 17
  • 141
  • 172
13

Make sure there is an namespace definition (xmlns) for the namespace your control belong to.

xmlns:myControls="clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName"
<myControls:thecontrol/>
ASh
  • 34,632
  • 9
  • 60
  • 82
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • 8
    +1 for "sometimes intellisense is stupid.". Make sure that the project actually doesn't compile and run, I've lost track of the number of times VS has told me my xmal is invalid when all it needed was a rebuild to make it reconsider. – Martin Harris Jul 07 '09 at 16:47
  • @MartinHarris yes! Also, I put my control in a Grid and initially forgot to give it a Grid.Row/Grid.Column assignment and Grid.RowSpan/Grid.ColumnSpan. – orangecaterpillar May 06 '20 at 23:11
7

This is how I got it to work:

User Control WPF

<UserControl x:Class="App.ProcessView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

    </Grid>
</UserControl>

User Control C#

namespace App {
    /// <summary>
    /// Interaction logic for ProcessView.xaml
    /// </summary>
    public partial class ProcessView : UserControl // My custom User Control
    {
        public ProcessView()
        {
            InitializeComponent();
        }
    } }

MainWindow WPF

<Window x:Name="RootWindow" x:Class="App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:App"
        Title="Some Title" Height="350" Width="525" Closing="Window_Closing_1" Icon="bouncer.ico">
    <Window.Resources>
        <app:DateConverter x:Key="dateConverter"/>
    </Window.Resources>
    <Grid>
        <ListView x:Name="listView" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <app:ProcessView />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>
Maz T
  • 1,184
  • 13
  • 18