【问题标题】:WPF - Use one main window for other windowsWPF - 将一个主窗口用于其他窗口
【发布时间】:2019-06-15 00:00:36
【问题描述】:

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

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

类似于 www.wpftutorial.net 这个网站。

有可能吗?

【问题讨论】:

标签: c# wpf


【解决方案1】:

考虑以下方法

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

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

然后将您的屏幕创建为 UserControls,当用户单击您的菜单或导航栏时,您可以在主窗口网格中添加一个用户控件作为子项。

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

这可以使用 MVVM 模式来完成

【讨论】:

  • 是主窗口中的网格名称,我更新了答案
  • 非常感谢亚历克斯。我在你的帮助下修好了。
  • 在主窗口内你需要有一个更复杂的结构。也许您需要添加一个面板(Dock Panel,Stack Panel)并在里面放置两个网格(或您需要的数量)一个用于菜单,一个用于控件,并且只使用用户控件清除网格
【解决方案2】:

听起来 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>

源代码和图片来自:

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

更多示例:

Different views/usercontrols on each tab of a TabControl

UserControl 和 Window 之间的区别

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)

【讨论】:

  • 谢谢 Terrance 的回答,但使用选项卡控件,我必须将所有代码写入同一个 cs 文件而不是单个文件。
  • 您可以制作您想要的不同页面 UserControls 而不是 Windows 并在 Xaml 中引用它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 2010-11-20
  • 2013-01-11
  • 1970-01-01
  • 2015-08-05
  • 2017-09-06
相关资源
最近更新 更多