Add a user control to a wpf window

后端 未结 4 1783
悲&欢浪女
悲&欢浪女 2020-12-02 19:38

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 t

相关标签:
4条回答
  • 2020-12-02 20:05

    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>
    
    0 讨论(0)
  • 2020-12-02 20:07

    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/>
    
    0 讨论(0)
  • 2020-12-02 20:11

    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>
    
    0 讨论(0)
  • 2020-12-02 20:26

    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 ..... />
    
    0 讨论(0)
提交回复
热议问题