首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"ContentControl“的内容必须是单个元素。

"ContentControl“的内容必须是单个元素。
EN

Stack Overflow用户
提问于 2021-05-12 18:18:34
回答 1查看 193关注 0票数 0

我是新来的WPF的。我想连接到电子图纸。我使用了以下指令:

https://www.codestack.net/edrawings-api/gettings-started/wpf/

我已经在works窗体中完成了该实现,它工作得非常完美。在Wpf中,我得到以下错误:

"ContentControl“的内容必须是单个项目

我在这里找到了一些解决方案。但不幸的是,没有什么能解决我的问题。

此外,以下是“守则”(与类似守则相同):

代码语言:javascript
复制
namespace PDM
{

public partial class eDrawingsHostControl : UserControl
{

    private EModelViewControl m_Ctrl;
    public eDrawingsHostControl()
    {
        InitializeComponent();

        var host = new WindowsFormsHost();
        var ctrl = new eDrawingHost();
        ctrl.ControlLoaded += OnControlLoaded;
        host.Child = ctrl;
        this.AddChild(host); // --- Here I got the error ---

    }   

    public string FilePath
    {
        get { return (string)GetValue(FilePathProperty); }
        set { SetValue(FilePathProperty, value); }
    }

    public static readonly DependencyProperty FilePathProperty =
        DependencyProperty.Register(nameof(FilePath), typeof(string),
            typeof(eDrawingsHostControl), new FrameworkPropertyMetadata(OnFilePathPropertyChanged));

    private static void OnFilePathPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as eDrawingsHostControl).OpenFile(e.NewValue as string);
    }

    private void OpenFile(string filePath)
    {
        if (m_Ctrl == null)
        {
            throw new NullReferenceException("eDrawings control is not loaded");
        }

        if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
        {
            m_Ctrl.CloseActiveDoc("");
        }
        else
        {
            m_Ctrl.OpenDoc(filePath, false, false, false, "");
        }
    }

    private void OnControlLoaded(EModelViewControl ctrl)
    {
        m_Ctrl = ctrl;
        m_Ctrl.OnFinishedLoadingDocument += OnFinishedLoadingDocument;
        m_Ctrl.OnFailedLoadingDocument += OnFailedLoadingDocument;
    }

    private void OnFailedLoadingDocument(string fileName, int errorCode, string errorString)
    {
        Trace.WriteLine($"{fileName} failed to loaded: {errorString}");
    }

    private void OnFinishedLoadingDocument(string fileName)
    {
        Trace.WriteLine($"{fileName} loaded");
    }
}

以下是xaml代码:

代码语言:javascript
复制
<Window x:Class="PDM.Edrawing"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:myControls="clr-namespace:PDM"
    mc:Ignorable="d"
    Title="Edrawing" Height="450" Width="800">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <myControls:eDrawingsHostControl Grid.Row="0" FilePath="{Binding Path=Text, ElementName=txtFilePath, UpdateSourceTrigger=Explicit}"/>
    <TextBox Grid.Row="1" x:Name="txtFilePath"/>
</Grid>

非常感谢你的帮助:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-12 18:56:58

您必须将this.AddChild(host);替换为this.Content=host;。请注意,这样的方式主机将是唯一的内容。

如果您想在UserControl中有一些额外的控件,您必须定义带有这些控件的ControlTemplate,例如,在其中包含ContentPresenter

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67509063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档