Reputation: 1
I wrote this method did not work
UserControl us = new UserControl();
us.Show();
Upvotes: 0
Views: 121
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
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