简体   繁体   中英

WPF - Use one main window for other windows

I am new to WPF and I would like to have one main window and a few other windows ( Settings, Forms etc.) Normally if the user clicks the Settings button, it opens Setting windows, if the user clicks other buttons than windows opens new windows what user selected.

BUT I want to show only one window to the user for all other windows. when he click Settings on the menu, Settings windows will be loaded to Main Windows. If the user selects any other windows than that windows will be loaded to the main windows.

similar to www.wpftutorial.net this website.

Is it possible ?

Consider the following approach

Inside your Main window put a Grid:

 <Window Name="mainWindow"
        x:Class="Example"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

        <Grid Name="grControls" />

   </Window>

then create your screens as UserControls and when the user clicks on your menu or navigation bar, you can add an user control inside Main Window grid as a child.

var userControl= new UserControl();
mainWindow.grControls.Children.Add(userControl);

This can be done using MVVM pattern

在此处输入图片说明

It sounds like a TabControl would give you what you want. Specifically, the property TabStripPlacement="Left" .

Example:

<Window x:Class="WpfTutorialSamples.Misc_controls.TabStripPlacementSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TabStripPlacementSample" Height="200" Width="250">
    <Grid>
        <TabControl TabStripPlacement="Left">
            <TabItem Header="General">
                <Label Content="Content goes here..." />
            </TabItem>
            <TabItem Header="Security" />
            <TabItem Header="Details" />
        </TabControl>
    </Grid>
</Window>

User Controls as View Content

If you change your child content pages from Windows to UserControls and refer to those user controls that should keep your codebase relatively clean.

Example : Your user control with the "view" content. (Yes mvvvm databinding should work fine for this)

View

The view you would want render as a TabItem

<!-- Your SecurityView.xaml; -->
<UserControl
        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"
        mc:Ignorable="d">
    <StackPanel>
        <TextBox Text="Security View" />        
    </StackPanel>
</UserControl>

MainWindow The "main" window used to hold all of your tabs.

<!-- Your MainWindow -->
<Window x:Class="WpfTutorialSamples.Misc_controls.TabStripPlacementSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyApp"
        Title="TabStripPlacementSample" Height="200" Width="250">
    <Grid>
        <TabControl TabStripPlacement="Left">
            <TabItem Header="General">
                <Label Content="Content goes here..." />
            </TabItem>
            <TabItem Header="Security" >
                <local:SecurityView />
            </TabItem>
        </TabControl>
    </Grid>
</Window>

在此处输入图片说明

Source code and picture from :

https://www.wpf-tutorial.com/tabcontrol/tab-positions/

For more examples:

Different views/usercontrols on each tab of a TabControl

Diff between UserControl and Window

Window vs Page vs UserControl for WPF navigation?

For more on TabControl:

( https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.tabcontrol?view=netframework-4.7.2 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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