Reputation: 17
Im new in WPF. I want to connect to Edrawings. I used the following instruction:
https://www.codestack.net/edrawings-api/gettings-started/wpf/
I did the implementation already in windowsforms which works perfectly. In Wpf i get the following error:
"The content of the "ContentControl" must be a single item"
I found some solution stuff here. But unfortunately nothing is working for my problem.
In addition, here is the Code (same as in the like):
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");
}
}
And here is the xaml code:
<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>
Thank you very much for your help :)
Upvotes: 0
Views: 416
Reputation: 7325
You have to replace this.AddChild(host);
with this.Content=host;
. Be aware, that such a way host will be the only content.
If you want to have some additional controls in the UserControl
you will have to define the ControlTemplate
with these controls and e.g. ContentPresenter
in it.
Upvotes: 1