Pergunta

EDIT: To introduce quickly: WindowsFormsHost added by xaml has all contents. Added by C# has no contents---> pictures below.

When I insert a WindowsFormsHost in the WrapPanel x:Name = "VideoPanel" by the XAML and then add VideoStream videoStream in the .cs file, conents of VideoStream are visible in the that WrapPanel. Everything is OK contents are visible(screenshot below).

Adnotation: VideoStream extends System.Windows.Forms.UserControl

XAML:

   <WrapPanel x:Name="VideoPanel" >
               <Border BorderBrush="Green" BorderThickness="2">
                        <WindowsFormsHost x:Name="Host" Width="400" Height="400"></WindowsFormsHost>
                    </Border>
   </WrapPanel>

.cs File:

VideoStream videoStream = new VideoStream();
Host.Child = VideoStream;

Result(correct result):

enter image description here


But when I want to create and insert WindowsFormsHost and the VideoStream in the WrapPanel by C# code there is only border visible with no contents. How to make contents of VideoStream visible?

Code with which I have problem:

   WindowsFormsHost formsHost = new WindowsFormsHost();
        VideoStream videoStream = new VideoStream();
        formsHost.Child = videoStream;
        Border lineBorder = new Border();
        lineBorder.BorderBrush = Brushes.Green;
        lineBorder.BorderThickness = new Thickness(2);
        lineBorder.Child = new WindowsFormsHost();
        VideoPanel.Children.Add(lineBorder);
        videoStream.Height = 400;
        videoStream.Width = 400;
        lineBorder.Width = 400;
        lineBorder.Height = 400;
        formsHost.Width = 400;
        formsHost.Height = 400;

XAML:

  <WrapPanel x:Name="VideoPanel" >
            </WrapPanel>

Result(incorrect):

enter image description here

How to make contents of VideoStream visible?

Foi útil?

Solução

Your problem is on this line :

lineBorder.Child = new WindowsFormsHost();

Should be :

lineBorder.Child = formsHost;

You're creating a new windowsformshost and not using the proper one.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top