C# 如何使用按钮单击从另一个 xaml 窗口打开一个 xaml 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19504087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I open a xaml window from another xaml window using button click?
提问by user2864566
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
Win1 OP= new Win1();
OP.show();
}
OP.show() is throwing an error.
OP.show() 抛出错误。
It is a usercontrol form.
它是一个用户控件表单。
采纳答案by XAMeLi
You say that Win1
is "It is a usercontrolform." (emphasis is mine).
您说的Win1
是“这是一个用户控件表单”。(重点是我的)。
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.
如果Win1
实际上是 type UserControl
,问题是该类型UserControl
没有定义Show()
方法。所以它不能作为一个窗口“打开”。
To solve this you need to open a window and have the UC as the content for that window:
要解决此问题,您需要打开一个窗口并将 UC 作为该窗口的内容:
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.
作为旁注,您可以在 App.xaml 中使用UserControl
asStartupUri
并且它会起作用,因为框架识别出它不是一个窗口并为其创建一个窗口。
回答by Cornel Marian
Opens a window and close the first one :
打开一个窗口并关闭第一个窗口:
private void Button_Click(object sender, RoutedEventArgs e)
{
window2 win2= new window2();
win2.Show();
this.Close();
}
回答by J R B
You can't show usercontrol directly, you should render your usercontrol in another window.
您不能直接显示用户控件,您应该在另一个窗口中呈现您的用户控件。
like.
喜欢。
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
UserControl1 uc1 = new UserControl1();
var win = new MainWindow();
win.Content = win1;
win.Show();
}
回答by S.Mishra
You can't open UserControl as Window or dialog box. Better add usercontrol into some Window through code behind or XAML and then open that window. Don't forget to set DataContext of that window. Remember datacontext of parent control/window will get inherited by child controls/usercontrols.
您不能将 UserControl 作为窗口或对话框打开。最好通过隐藏代码或 XAML 将用户控件添加到某个窗口中,然后打开该窗口。不要忘记设置该窗口的 DataContext。请记住父控件/窗口的数据上下文将被子控件/用户控件继承。
回答by Anthony Smyth
If you'd like to do it like a popup:
如果您想像弹出窗口一样执行此操作:
private void btn1_click(object sender, RoutedEventArgs e)
{
newWin win2 = new newWin();
win2.Show():
}
回答by Iridium237
All of these answers are fine, however they do not take advantage of WPF's Navigationfeatures. If you're going to be hiding/closing the current window anyway, then this is a great alternative to the old WinForms
window management. Instead of creating multiple windows, create pages and simply display them using a single Frame control in the MainWindow:
所有这些答案都很好,但是它们没有利用WPF 的导航功能。如果您无论如何要隐藏/关闭当前窗口,那么这是旧WinForms
窗口管理的一个很好的替代方案。不是创建多个窗口,而是创建页面并使用 MainWindow 中的单个 Frame 控件简单地显示它们:
<Grid>
<Frame Source="/Menu.xaml" NavigationUIVisibility="Hidden"/>
</Grid>
Then when you want to display another "window" (actually a page), you can use NavigationService
to change the frame source. From the Menu page code behind:
然后当你想显示另一个“窗口”(实际上是一个页面)时,你可以使用NavigationService
改变框架源。从后面的菜单页面代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Contact.xaml", UriKind.Relative));
}
This will now display the Contact page. Using this method ensures that the window properties are standardized. One window to rule them all!
现在将显示“联系人”页面。使用此方法可确保窗口属性标准化。一扇窗,统统统统!