C# WPF Child Windows inside Main Window

2019-06-14 09:10发布

So iv looked around for a bit and found out that MDI is obselete for WPF, Basically what i am trying to do is show a specific page in a grid object on load, and once a menu item from my drop down menu is selected, the content of the grid will be changed to the content from a different page (this is depending on which menu item is selected).

To go into more detail (perhaps this will help) The area where the window will be shown will need to have the window with no borders, or titles, or buttons to minimize/close etc.. only showing the content of this window, it won't be resizeable but fixed, i have a menu of which as i said earlier, when a different menu item is clicked, the relevant window should be displayed in the fixed area. Additionally if any buttons or events inside this content that is displayed happen (i.e a button causes a different window to show for example) then the content in the fixed area should be replaced by this new window's content

This is the first time i have done something like this and from what i've read it sounds like this is something very tricky for a WPF application, I hope i can get some sort of insight or direction i should be going so that i can make this possible.

Thanks.

1条回答
爷的心禁止访问
2楼-- · 2019-06-14 10:02

You can try for example ChildWindow from Extended WPF Toolkit Community Edition.

Edit #1:

But whenever i try to create a WindowContainer in the Xaml it has problems with the namespace prefix with "xctk:WindowContainer" so how do i create the appropriate namespace prefix to use it?

You have to add that namespace:

xmlns:xctk=http://schemas.xceed.com/wpf/xaml/toolkit

For example:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <xctk:WindowContainer>
            <xctk:ChildWindow Height="100" Width="250" Left="10" Top="10">
                <TextBlock Text="Hello World ..." />
            </xctk:ChildWindow>
        </xctk:WindowContainer>
    </Grid>
</Window>

Edit #2:

You can of course change some properties (for example):

<xctk:ChildWindow
                Height="100"
                Width="250" 
                Left="10" 
                Top="10"
                Name="chWindow"
                CloseButtonVisibility="Hidden"
                WindowStyle="None"
                BorderThickness="0">

Edit #3:

Ok yeah, so with everything referenced it is giving me errors still..

Try it simple… Create Wpf Application, add Extended WPF Toolkit 2.4 NuGet package, in MainWindow.xaml add previous code and in MainWindow.xaml.cs add next code:

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

            this.Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.chWindow.Show();
        }
    }
}
查看更多
登录 后发表回答