简体   繁体   English

有没有办法通过单击 Form1 按钮在 MainWindow 上显示 Form2

[英]Is there a way to show Form2 on MainWindow from button click on Form1

I have managed to get Form1 to open in my grid on the MainWindow but now I need to close Form1 and show Form2 in the same grid on the MainWindow after I click a button on Form1.我已经设法让 Form1 在 MainWindow 的网格中打开,但现在我需要在单击 Form1 上的按钮后关闭 Form1 并在 MainWindow 的同一网格中显示 Form2。

How can this be done?如何才能做到这一点?

MainWindow Code:主窗口代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Configuration;
using Application = System.Windows.Application;
using System.Windows.Forms.Integration;

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

        public void BtnFuel_Click(object sender, RoutedEventArgs e)
        {
            FuelDash fd1 = new FuelDash();
            fd1.TopLevel = false;

            WindowsFormsHost host1 = new WindowsFormsHost();
            host1.Child = fd1;

            stackForms.Children.Add(host1);            
        }
     }
}

The MainWindow Design Code is: MainWindow 设计代码为:

<Window x:Class="SolAquaPro.WinHomeDash"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:local="clr-namespace:SolAquaPro"
        mc:Ignorable="d"
        Title="winHomeDash" Height="650" Width="950"
        WindowStartupLocation="CenterScreen"
        WindowStyle="None"
        materialDesign:ThemeAssist.Theme="Dark"
        Background="{x:Null}" Loaded="BtnMenuShort_Click">

    <materialDesign:Card UniformCornerRadius="15" Margin="25" Width="900" Height="600" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}">
        
        <materialDesign:Card UniformCornerRadius="15" HorizontalAlignment="Left" Width="225" Background="#0A1828">
            
            <DockPanel Width="225" Height="600">
                <StackPanel Height="155" VerticalAlignment="Top" Width="225">
                    <Image Height="50" Width="50" Margin="0 15 0 15" Source="/UserPic.png"/>
                    <Label Name="lblName" BorderThickness="0" FontFamily="Arial" FontWeight="Bold" FontSize="17" Margin="0 0 0 5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Height="24" Loaded="Window_Loaded"/>
                    <Label x:Name="lblDateTime" Content="" FontFamily="Arial" FontWeight="SemiBold" FontSize="12" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Margin="0 0 0 18" Height="22" Width="225" Loaded="LblDateTime_Loaded"/>
                    <Rectangle Name="rec1" Height="7" Fill="#FF2372FA"/>
                </StackPanel>
                <materialDesign:Card Name="card2" Width="210" UniformCornerRadius="15" Height="445" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}" Foreground="White" Margin="-210,155,0,0">

                    <StackPanel VerticalAlignment="Center" Height="445" Name="stpnlMenu">
                        <Button x:Name="btnCollapse" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="RightToLeft" Margin="195,207.5,0,0" Click="BtnMenuShort_Click">
                            <Button.Background>
                                <ImageBrush ImageSource="/more_than_25px.png"/>
                            </Button.Background>
                        </Button>
                        <Button x:Name="btnMenu" Content="MENU" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-227.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" Click="BtnMenuShort_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnHome" Content="HOME" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-187.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnFuel" Content="FUEL" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-147.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu" Click="BtnFuel_Click"/>
                        <Button x:Name="btnTyres" Content="TYRES" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnService" Content="SERVICE" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-97.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnBags" Content="WASTE BAGS" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-72.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnExpand" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="LeftToRight" Margin="25,-30,0,0" Click="BtnMenuShort_Click" Visibility="Hidden">
                            <Button.Background>
                                <ImageBrush ImageSource="/more_than_25px.png"/>
                            </Button.Background>
                        </Button>
                        <Button x:Name="btnChat" Content="QUICK CHAT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnHelp" Content="SUPPORT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnLogOut" Content="LOG OUT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,20" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" Click="BtnLogOut_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>

                    </StackPanel>
                </materialDesign:Card>

                <StackPanel Width="30" Height="445" Margin="-400,155,0,0" Background="#0A1828">
                    <Rectangle Name="rec2" Height="10"/>
                    <Button Name="btnMenuShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Click="BtnMenuShort_Click" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/menu_30px.png" Stretch="Uniform"/>
                        </Button.Background>
                    </Button>

                    <Rectangle Name="rec3" Height="20"/>
                    <Button Name="btnHomeShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/home_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Rectangle Name="rec4" Height="20"/>
                    <Button Name="btnFuelShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand" Click="BtnFuel_Click">
                        <Button.Background>
                            <ImageBrush ImageSource="/gas_station_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnTyresShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/wheel_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnServiceShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/maintenance_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnBagsShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/waste_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Rectangle Name="rec5" Height="170"/>
                    <Button Name="btnChatShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/chat_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnHelpShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/help_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnLogOutShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Click="BtnLogOut_Click" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/exit_sign_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>
                    <Rectangle Name="rec6" Height="10"/>

                </StackPanel>

                <materialDesign:Clock Name="clClock" Width="675" Height="22" FontFamily="Arial" FontWeight="SemiBold" FontSize="10" Foreground="White" Margin="0,578,0,0"/>
                <Grid  Width="625" Height="550" Margin="25,25,25,25" Name="stackForms" Background="#FF292929">
                    
                </Grid>

            </DockPanel>            
                
        </materialDesign:Card>      

    </materialDesign:Card>
</Window>

Form1(FuelDash) loads perfectly into stackForms but now I need to Load Form2(FuelEntry) into stackForms when I click BtnFuelLoad . Form1(FuelDash) 完美加载到stackForms但现在我需要在单击stackForms时将 Form2(FuelEntry) 加载到BtnFuelLoad

Form1 code is: Form1代码是:

using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using static SolAquaPro.WinHomeDash;

namespace SolAquaPro
{
    public partial class FuelDash : Form
    {
        public FuelDash()
        {
            InitializeComponent();

        }

        private void Timer1_Tick(object sender, EventArgs e)
        {

        }

        private void BtnFuelLoad_Click(object sender, EventArgs e)
        {
            this.Close();
            


        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

I know I need to reference an object in WinHomeDash but I keep getting need to reference non static object.我知道我需要在 WinHomeDash 中引用WinHomeDash ,但我一直need to reference non static object.

Window Forms has nothing to do with a form in sense of GUI. Window Forms 与 GUI 意义上的形式无关。 It's a platform.这是一个平台。 WPF is the successor of Windows Forms. WPF 是 Windows Forms 的继任者。

In WPF you extend Window instead of Form.在 WPF 中,您扩展Window而不是 Form。 Just delete all references to Windows Forms.只需删除所有对 Windows Forms 的引用即可。
Since you are trying ti implement a singe page application, you would replace the Grid with a Frame .由于您正在尝试实现单页应用程序,因此您可以将Grid替换为Frame Then let all your pages extend Page .然后让您的所有页面扩展Page

WinHomeDash.xaml WinHomeDash.xaml

<Window>
  <StackPanel>
    <!-- Navigation pane -->
    <StackPanel Orientation="Horizontal">
     
      <!-- Example how to navigate from outside the pages -->

      <Button Click="LoadForm1_OnClick" Content="Form 1" />
      <Button Click="LoadForm2_OnClick" Content="Form 2" />
    </StackPanel>

    <Frame x:Name="stackForms" 
           NavigationUIVisibility="Hidden" />
  </StackPanel>
</Window>

WinHomeDash.xaml.cs WinHomeDash.xaml.cs

partial class WinHomeDash : Window
{
  private void LoadForm1_OnClick(object sender, RoutedEventArgs e)
  {
    Page form1 = new Form1();
    this.stackForms.Navigate(form1);
  }

  private void LoadForm2_OnClick(object sender, RoutedEventArgs e)
  {
    Page form2 = new Form2();
    this.stackForms.Navigate(form2);
  }
}

Then add a new item the project and select Page .然后添加一个新项目项目和 select Page This creates a.xaml file with a nested file containig the partial class code-behind file:这将创建一个包含部分 class 代码隐藏文件的嵌套文件的 .xaml 文件:

Form1.xaml表格1.xaml

<Page>
  <StackPanel>
    <TextBlock Text="This is the first page." />

    <!-- Example how to navigate from within the page -->
    <Button Content="Navigate to next page" 
            Click="LoadForm2_OnClick" />
</Page>

Form1.xaml.cs Form1.xaml.cs

public partial class Form1 : Page
{
  public Form1()
  {
    InitializeComponent();
  }

  private void LoadForm2_OnClick(object sender, RoutedEventArgs e)
  {
    Page form2 = new Form2();
    this.NavigationService.Navigate(form2);
  }
}

Form2.xaml表格2.xaml

<Page>
  <StackPanel>
    <TextBlock Text="This is the first page." />

    <!-- Example how to navigate from within the page -->
    <Button Content="Navigate to previous page" 
            Click="LoadPreviousForm_OnClick" />
</Page>

Form2.xaml.cs Form2.xaml.cs

public partial class Form2 : Page
{
  public Form1()
  {
    InitializeComponent();
  }

  private void LoadPreviousForm_OnClick(object sender, RoutedEventArgs e)
  {    
    // Navigate to previous page using the history
    this.NavigationService.GoBack()

    // Navigate to previous page using an explicit content
    Page form1 = new Form1();
    this.NavigationService.Navigate(form1);
  }
}

Since Frame is a heavyx control as it also supports to render web sources, you should use a ContentControl an DataTemplate to implement the page hosting:由于 Frame 是一个重型控件,因为它还支持呈现 web 源,因此您应该使用 ContentControl 和 DataTemplate 来实现页面托管:
C# WPF Navigation Between Pages (Views) C# WPF 页面间导航(视图)
How do I toggle between pages in a WPF application?如何在 WPF 应用程序中的页面之间切换?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM