将用户控件添加到 wpf 窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1093429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a user control to a wpf window
提问by WedTM
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.
我有一个我创建的用户控件,但是当我将它添加到窗口中的 XAML 时,Intellisense 没有选择它,我不知道如何将它添加到窗口中。
回答by Martin Harris
You need to add a reference inside the window tag. Something like:
您需要在 window 标签内添加一个引用。就像是:
xmlns:controls="clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName"
(When you add xmlns:controls=" intellisense should kick in to make this bit easier)
(当您添加 xmlns:controls=" 时,智能感知应该启动以使这更容易一些)
Then you can add the control with:
然后你可以添加控件:
<controls:CustomControlClassName ..... />
回答by user7116
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>
回答by Muad'Dib
Make sure there is an namespace definition (xmlns
) for the namespace your control belong to.
确保xmlns
您的控件所属的命名空间有一个命名空间定义 ( )。
xmlns:myControls="clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName"
<myControls:thecontrol/>
回答by Ma?vydas Tadaravi?ius
This is how I got it to work:
这就是我让它工作的方式:
User Control WPF
用户控制 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#
用户控制 C#
namespace App {
/// <summary>
/// Interaction logic for ProcessView.xaml
/// </summary>
public partial class ProcessView : UserControl // My custom User Control
{
public ProcessView()
{
InitializeComponent();
}
} }
MainWindow WPF
主窗口 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>