【问题标题】:Add a user control to a wpf window将用户控件添加到 wpf 窗口
【发布时间】:2019-10-15 21:13:19
【问题描述】:

我已经创建了一个用户控件,但是当我将它添加到窗口中的 XAML 时,Intellisense 没有选择它,我不知道如何将它添加到窗口.

【问题讨论】:

    标签: wpf user-controls


    【解决方案1】:

    您需要在 window 标签内添加一个引用。比如:

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

    (当您添加 xmlns:controls=" 时,智能感知应该会启动以使这更容易)

    然后你可以添加控件:

    <controls:CustomControlClassName ..... />
    

    【讨论】:

    • 现在是2015年了,设计师还没有自动做这个? WTF!?!
    • @MartinHarris 我应该在“yourassemblyname”中写什么?
    • 显然是您的程序集名称 - 这是输出文件的名称(主要是 dll)。如果输出 dll 的名称为 awesome.assembly.dll,则“yourassemblyname”为“awesome.assembly”。
    • VS2013 设计师更喜欢省略了组装
    • 我如何知道程序集名称
    【解决方案2】:

    您可能需要添加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>
    

    【讨论】:

      【解决方案3】:

      确保您的控件所属的命名空间有一个命名空间定义 (xmlns)。

      xmlns:myControls="clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName"
      
      <myControls:thecontrol/>
      

      【讨论】:

      • +1 表示“有时智能感知很愚蠢。”。确保项目实际上没有编译和运行,我已经忘记了 VS 告诉我我的 xmal 无效的次数,而它所需要的只是重建以使其重新考虑。
      • @MartinHarris 是的!另外,我把我的控件放在一个 Grid 中,最初忘记给它一个 Grid.Row/Grid.Column 分配和 Grid.RowSpan/Grid.ColumnSpan。
      【解决方案4】:

      这就是我让它工作的方式:

      用户控制 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>
      

      用户控制 C#

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

      主窗口 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>
      

      【讨论】:

      • 哇,你太棒了!非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2021-11-12
      • 2021-04-17
      • 2014-12-15
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多