如何将 UserControl 添加到 WPF 窗口上的面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11231649/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:27:25  来源:igfitidea点击:

How to add UserControl to a Panel on a WPF Window

wpfxamluser-controls

提问by IanGilham

I think I'm missing something that should be obvious here, but I'm drawing a blank on this one.

我想我在这里遗漏了一些应该很明显的东西,但我在这个上面画了一个空白。

I've built a very primitive UserControl containing nothing more than a TextBoxto use as a log window:

我已经构建了一个非常原始的 UserControl 只包含一个TextBox用作日志窗口:

<UserControl x:Class="My.LoggerControl"
             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"
             x:Name="LoggerView">
    <Grid x:Name="LayoutRoot">
        <TextBox x:Name="LogWindow" AcceptsReturn="True"/>
    </Grid>
</UserControl>

I don't expect that to be the best way to do it, but it should be good enough for a prototype.

我不认为这是最好的方法,但对于原型来说应该足够了。

The code-behind is similarly simple:

代码隐藏同样简单:

public partial class LoggerControl : UserControl, ILogger
{
    public LoggerControl()
    {
        InitializeComponent();
    }

    private LogLevel level = LogLevel.Warning;

    #region ILogger

    public LogLevel Level
    {
        get { return level; }
        set { level = value; }
    }

    public void OnError(string s)
    {
        if (level >= LogLevel.Error)
            LogWindow.AppendText("ERROR:::" + s + "\n");
    }

    // ...
    #endregion
}

The thing I can't figure out is how to add this control to my MainWindow.xaml. Simplifying, lets say my window looks like this:

我无法弄清楚的是如何将此控件添加到我的MainWindow.xaml. 简化一下,假设我的窗口如下所示:

<Window x:Class="My.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:My"
    Title="Test" Height="350" Width="525">
    <Grid>
        <local:LoggerControl x:Name="LogView" />
    </Grid>
</Window>

Even with something so simple, the Designer in Visual Studio 2010 can't load the main window. The error given is:

即使有这么简单的事情,Visual Studio 2010 中的设计器也无法加载主窗口。给出的错误是:

A value of type 'LoggerControl' cannot be added to a collectionor dictionary of type 'UIElementCollection'.

无法将“LoggerControl”类型的值添加到“UIElementCollection”类型的集合或字典中。

This error message has only one unrelated hit in the major search engines (plus duplicates) so I haven't found any useful help. Microsoft's own documentation seems to imply that this should work.

此错误消息在主要搜索引擎中只有一个不相关的命中(加上重复项),所以我没有找到任何有用的帮助。微软自己的文档似乎暗示这应该有效。

Any idea how to solve this?

知道如何解决这个问题吗?

回答by cunningdave

<UserControl x:Class="My.LoggerControl"


 xmlns:local="clr-namespace:My.LogTest"

Looks like you may have made a mistake in the namespacing? LoggerControl is listed as being the namespace My, while you're importing My.LogTest and assigning it to the xml-prefix local. Change this to:

看起来您可能在命名空间中犯了错误?LoggerControl 被列为命名空间 My,而您正在导入 My.LogTest 并将其分配给 xml-prefix local。将此更改为:

xmlns:local="clr-namespace:My"

And I think it should work. Otherwise, fix the LoggerControl declaration.

我认为它应该有效。否则,修复 LoggerControl 声明。