Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a xaml window from another xaml window using button click?

Tags:

c#

xml

xaml

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Win1 OP= new Win1();
            OP.show();
        }

OP.show() is throwing an error.

It is a usercontrol form.

like image 846
user2864566 Avatar asked Oct 21 '13 20:10

user2864566


1 Answers

You say that Win1 is "It is a usercontrol form." (emphasis is mine).

If Win1 is actually of type UserControl, the issues is that the type UserControl doesn't define Show() method. So it cannot be "opened" as a window.

To solve this you need to open a window and have the UC as the content for that window:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
    Win1 OP= new Win1();
    var host = new Window();
    host.Content = OP;
    host.Show();
}

As a side note, you can use UserControl as StartupUri in App.xaml and it will work since the framework recognizes that it's not a window and creates a window for it.

like image 143
XAMeLi Avatar answered Sep 18 '22 23:09

XAMeLi