【问题标题】:Displaying a form in a window panel in WPF application在 WPF 应用程序的窗口面板中显示表单
【发布时间】:2022-01-24 04:46:51
【问题描述】:

所以我创建了一个带有空间的窗口来显示和显示每个表单,具体取决于单击哪个按钮,问题是我似乎无法找到任何解决方案来解决如何在特定窗口或面板中打开表单那个特定的窗口。

这可能吗?

我的Window设计代码如下:

Title="winHomeDash" Height="650" Width="950"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
AllowsTransparency="True"
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="645" Height="550" Margin="0,25,25,25" Name="stackForms">
            
        </Grid>

    </DockPanel>            
        
</materialDesign:Card>      

</materialDesign:Card>

网格的位置是我希望显示表单的位置。 Window cs代码如下:

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;

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

        private void LblDateTime_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void BtnLogOut_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            Window main = new MainWindow();
            main.Show();
        }

        public void BtnMenuShort_Click(object sender, RoutedEventArgs e)
        {
            if ((card2.Visibility == Visibility.Visible) && (btnCollapse.Visibility == Visibility.Visible))
            {
                card2.Width = 45;
                card2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                
                btnBags.Visibility = Visibility.Hidden;
                btnChat.Visibility = Visibility.Hidden;
                btnFuel.Visibility = Visibility.Hidden;
                btnHome.Visibility = Visibility.Hidden;
                btnMenu.Visibility = Visibility.Hidden;
                btnLogOut.Visibility = Visibility.Hidden;
                btnService.Visibility = Visibility.Hidden;
                btnHelp.Visibility = Visibility.Hidden;
                btnTyres.Visibility = Visibility.Hidden;
                btnCollapse.Visibility = Visibility.Hidden;
                
                btnExpand.Visibility = Visibility.Visible;

            }
            
            else
            {
                if ((card2.Visibility == Visibility.Visible) && (btnCollapse.Visibility == Visibility.Hidden))
                {
                    card2.Width = 210;
                    card2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    btnBags.Visibility = Visibility.Visible;
                    btnChat.Visibility = Visibility.Visible;
                    btnFuel.Visibility = Visibility.Visible;
                    btnHome.Visibility = Visibility.Visible;
                    btnMenu.Visibility = Visibility.Visible;
                    btnLogOut.Visibility = Visibility.Visible;
                    btnService.Visibility = Visibility.Visible;
                    btnHelp.Visibility = Visibility.Visible;
                    btnTyres.Visibility = Visibility.Visible;
                    btnCollapse.Visibility = Visibility.Visible;
                    
                    btnExpand.Visibility = Visibility.Hidden;
                }
            }            
        }

        public void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lblName.Content = Application.Current.Resources["UserAssign"].ToString();
        }

        private void BtnFuel_Click(object sender, RoutedEventArgs e)
        {           
            Form frmFuelDash = new FuelDash();
            frmFuelDash.Show(); 
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            DragMove();
        }
    }
}

提前谢谢你

【问题讨论】:

    标签: c# wpf visual-studio


    【解决方案1】:

    您可以尝试参考以下代码,看看它是否与您的匹配。 如果您有任何问题,请告诉我。

    右击项目下的引用,选择Add Reference...

    勾选System.Windows.FormsWindowsFormsIntegration并点击OK。

    项目结构:

    MainWindow.xaml:

    <DockPanel >
            <Grid x:Name="grid"  Background="LightYellow" Width="500" Height="400">
            </Grid>
            <StackPanel x:Name="sp1"   Width="200" Height="100" Background="LightSkyBlue" >
                <Button x:Name="btn1" Width="80" Height="30" Background="LightGray" Content="form1" Click="btn1_Click" />
                <Button x:Name="btn2" Width="80" Height="30" Background="LightGray" Content="form2" Click="btn2_Click"/>
            </StackPanel>
        </DockPanel>
    

    MainWindow.xaml.cs:

    using System.Windows;
    using System.Windows.Forms.Integration;
    
    namespace WpfShowForm
    {
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
        }
        private void btn1_Click(object sender, RoutedEventArgs e)
        {
          Form1 f1 = new Form1();
          f1.TopLevel = false;
          WindowsFormsHost host1 = new WindowsFormsHost();
         
          host1.Child=f1;
    
          grid.Children.Add(host1);
        }
    
        private void btn2_Click(object sender, RoutedEventArgs e)
        {
          Form2 f2 = new Form2();
          f2.TopLevel = false;
          WindowsFormsHost host2 = new WindowsFormsHost();
          host2.Child = f2;
          grid.Children.Add(host2);
        }
      }
    }
    

    在 Form1 中添加按钮:

    private void button1_Click(object sender, EventArgs e)
        {
          Form2 f2 = new Form2();
          f2.Show();
        }
    

    结果:

    【讨论】:

    • 谢谢你,我按照你说的做了,但也删除了 AllowsTransparency="true" 行,这意味着我的圆角和花哨的外观现在必须删除
    • 所以我还需要问一下,我让它在按钮单击时打开 form1,但我需要在从 Form1 中单击按钮时打开 Form2。我该怎么做?
    • 嗨,@TravO。我更新了答案,你可以看看。
    • 非常感谢,我刚刚从头开始使用 MVVM 架构做项目,我不会撒谎,有点学习曲线,但似乎值得,但谢谢你,因为这对我来说仍然是有用的信息@Hui Liu -MFST
    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    相关资源
    最近更新 更多