jamal kazemi
jamal kazemi

Reputation: 1

Viewing a form of WPF in Windows Forms C#

I wrote this method did not work

UserControl us = new UserControl();
us.Show();

Upvotes: 0

Views: 121

Answers (2)

Dax Pandhi
Dax Pandhi

Reputation: 853

You can't show a UserControl. Change your UserControl to a Window.

XAML:

<Window x:Class="WindowsFormsApplication1.MyWindow"

instead of

<UserControl x:Class="WindowsFormsApplication1.UserControl1"

and in your code-behind, change

public partial class UserControl1 : UserControl

to

public partial class MyWindow: Window

Now you can call new MyWindow().Show();. The major benefit is that you are not overburdening the application by adding a Windows Form dialog and an ElementHost and a UserControl inside it.

This way you can also access the children of the UserControl/Window from the calling Windows Form class.

Upvotes: 1

Emmanuel DURIN
Emmanuel DURIN

Reputation: 4913

Your control has to be located in a window

Window  Window = new Window();
// window has a single content
// here it is usercontrol1
// to have many controls, use an intermediary like Grid or Canvas or any Panel derived class
window.Content = usercontrol1;

The Window has to be opened.

// modeless (non blocking) opening
window.Show();

or

// modal (blocking) opening
window.Showdialog();

Regards

Upvotes: 1

Related Questions