Yoda
Yoda

Reputation: 18068

How to Add UserControl to UniformGrid in .cs

EDIT: When I want to add VideoStream directrly to the grid by: Grid.Children.Add(VideoStream); IDE says that this is wrong argument.

I have a class:

 public partial class VideoStream : UserControl 

And the MainWindow class I would like to add this UserControl to UniformGrid inserted in that Window:

<Window x:Class="HomeSecurity.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" WindowState="Maximized">

    <UniformGrid x:Name="Grid">
        <WindowsFormsHost x:Name="Host"></WindowsFormsHost>
    </UniformGrid>
</Window>

 public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            VideoStream VideoStream = new VideoStream();
          //  VideoStream.TopLevel = false; 
           // WindowsFormsHost Host = new WindowsFormsHost();
            try {
                Host.Child = VideoStream;
                Grid.Children.Add(Host); //THIS CAUSES THE EXCEPTION
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
            }

        }
    }

But I get exception:

A first chance exception of type 'System.ArgumentException' occurred in PresentationCore.dll
   w System.Windows.Media.VisualCollection.Add(Visual visual)
   w System.Windows.Controls.UIElementCollection.AddInternal(UIElement element)
   w System.Windows.Controls.UIElementCollection.Add(UIElement element)
   w HomeSecurity.MainWindow..ctor() w c:\Users\R\Documents\Visual Studio 2013\Projects\HomeSecurity\HomeSecurity\MainWindow.xaml.cs:wiersz 28
'HomeSecurity.vshost.exe' (CLR v4.0.30319: HomeSecurity.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationProvider\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationProvider.dll'. Symbols loaded.
'HomeSecurity.vshost.exe' (CLR v4.0.30319: HomeSecurity.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Symbols loaded.

How to correctly add this UserControl to the UniformGrid

Upvotes: 0

Views: 449

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

You can do that in XAML only:

<Window x:Class="HomeSecurity.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:NamespaceWhereVideoStreamDeclared"
        Title="MainWindow" WindowState="Maximized">

    <UniformGrid x:Name="Grid">
        <WindowsFormsHost x:Name="Host">
            <local:VideoStream/>
        </WindowsFormsHost>
    </UniformGrid>
</Window>

Declare namespace local at root level and replace NamespaceWhereVideoStreamDeclared with actual namespace where VideStream is declared.


If you want to do that in code behind, you don't need to add Host to Grid since Host is already child of Grid.

Grid.Children.Add(Host); // Remove this line and you are good to go.

Upvotes: 2

Related Questions