【发布时间】:2019-06-15 00:00:36
【问题描述】:
我是 WPF 新手,我想要一个主窗口和几个其他窗口(设置、表单等) 通常,如果用户单击设置按钮,则会打开设置窗口,如果用户单击窗口以外的其他按钮,则会打开用户选择的新窗口。
但是 对于所有其他窗口,我只想向用户显示一个窗口。当他单击菜单上的设置时,设置窗口将加载到主窗口。如果用户选择了除该窗口之外的任何其他窗口,则该窗口将被加载到主窗口。
类似于 www.wpftutorial.net 这个网站。
有可能吗?
【问题讨论】:
我是 WPF 新手,我想要一个主窗口和几个其他窗口(设置、表单等) 通常,如果用户单击设置按钮,则会打开设置窗口,如果用户单击窗口以外的其他按钮,则会打开用户选择的新窗口。
但是 对于所有其他窗口,我只想向用户显示一个窗口。当他单击菜单上的设置时,设置窗口将加载到主窗口。如果用户选择了除该窗口之外的任何其他窗口,则该窗口将被加载到主窗口。
类似于 www.wpftutorial.net 这个网站。
有可能吗?
【问题讨论】:
考虑以下方法
在主窗口中放置一个网格:
<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 模式来完成
【讨论】:
听起来 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>
<!-- 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 之间的区别 有关 TabControl 的更多信息:【讨论】:
UserControls 而不是 Windows 并在 Xaml 中引用它们