WPF-将一个主窗口用于其他窗口

时间:2019-01-21 15:02:11

标签: c# wpf

我是WPF的新手,我希望拥有一个主窗口和其他几个窗口(“设置”,“表单”等)。 通常,如果用户单击“设置”按钮,则会打开“设置”窗口,如果用户单击窗口以外的其他按钮,则会打开用户选择的新窗口。

但是 对于所有其他窗口,我只想向用户显示一个窗口。当他单击菜单上的“设置”时,“设置”窗口将被加载到主窗口。如果用户选择了其他窗口,则该窗口将被加载到主窗口。

类似于www.wpftutorial.net此网站。

有可能吗?

2 个答案:

答案 0 :(得分:1)

考虑以下方法

在主窗口内放置一个网格:

convertAndSend(...)

然后将屏幕创建为UserControls,然后当用户单击菜单或导航栏时,您可以将其作为子项添加到Main Window网格中。

 <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>

这可以使用MVVM模式完成

enter image description here

答案 1 :(得分:0)

听起来TabControl会给您想要的东西。具体而言,属性TabStripPlacement="Left"

示例:

<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>

用户控件作为视图内容

如果将子内容页面从Windows更改为UserControls,并引用应保持代码库相对干净的那些用户控件。

示例: 您的用户控件带有“视图”内容。 (是的,mvvvm数据绑定应该可以正常工作)

查看

您想要呈现为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用于保存所有选项卡的“主”窗口。

<!-- 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>

enter image description here

源代码和图片来自:

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

有关更多示例:

Different views/usercontrols on each tab of a TabControl

用户控件和窗口之间的差异

Window vs Page vs UserControl for WPF navigation?

有关TabControl的更多信息:

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