【问题标题】:WPF Button Click Leading to PageWPF按钮单击导致页面
【发布时间】:2018-09-20 15:00:17
【问题描述】:

我的主窗口中目前有 3 个按钮。按钮 1 我想在单击时指向 Page1.xaml。按钮 2 我想引导到 Page2.xaml 等等。我该怎么办?

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }



    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {


    }
}



}

【问题讨论】:

标签: c# html wpf


【解决方案1】:

我希望下面的代码会有所帮助:

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }



    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
      NavigationWindow window = new NavigationWindow();
      window.Source = new Uri("Page1.xaml", UriKind.Relative);
      window.Show();
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
      NavigationWindow window = new NavigationWindow();
      window.Source = new Uri("Page2.xaml", UriKind.Relative);
      window.Show();
    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
      NavigationWindow window = new NavigationWindow();
      window.Source = new Uri("Page3.xaml", UriKind.Relative);
      window.Show();
    }
}
}

您可以通过将 NavigationWindow 声明为全局实例并在构造函数中进行初始化来进一步优化,但这取决于您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多