1

I have the following MainWindow class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace mylibrary
{
    public class MainWindow : Window
    {
        WorkSpaceView ws;
        public MainWindow()
            : base()
        {
            ws = new WorkSpaceView();
        }

    }
}

In above code WorkSpaceView is a wpf user control. I need to add this user control to the MainWindow. But I don't know how!? Could you please guide me how should I add and show a user control inside a window, dynamically?

0

2 Answers 2

3

A WPF window has a Content property.

Typically, a WPF control that can have only one child or content item has this property.

If it can have multiple, it will likely use the Children property.

adding your control to the window should be as simple as :

public class MainWindow : Window
{
    WorkSpaceView ws;
    public MainWindow()
        : base()
    {
        ws = new WorkSpaceView();
        this.Content = ws;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

If you have a grid in your xaml file then you can do something like

 var myUsercontrol= new WorkSpaceView ();
 MyGrid.Children.Add(myUsercontrol);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.