如何在主窗口中将 WPF window 作为 TabControl 项加载?
How to Load a WPF window in the MainWindow as a TabControl item?
我的 MainWindow.xaml 的结构如下
<Window>
<Grid>
<TabControl Name="MainTabCntrl">
<TabItem1>
<TabItem2>
<TabItem3>
.
.
.
<TabItemN>
</TabControl>
</Grid>
</Window>
问题是我的MainWindow.xaml目前大约有4000行代码,效率不高(你同意吗?)
我要实现的解决方案是单独创建 N windows(代表我的 TabItems),每次用户单击 Tab 项时,我都会在该 TabItem 中加载相关的 windows,如下所示
private void inventory_start()//this function is called in my MainWinodw.xaml.cs
{
inv = new Inventory(db, logViewer);//this is a window
TabItem tbItem = new TabItem();
Frame frame = new Frame();
frame.Content = inv;
tbItem.Name = "invTab";
tbItem.Content = frame;
tbItem.IsSelected = true;
MainTabCntrl.Items.Add(tbItem);
inv.swithInventoryTabs("inv_info");
}
我现在有一个错误,“'Management_V0.Inventory' 根元素对于导航无效。”
A window 不能 是另一个元素的子元素。期间.
但是您可以将 Inventory
window 的内容移动到 UserControl
(例如,只需复制并粘贴 XAML 和其中的代码文件到另一个)并将这个文件用作 Inventory
window 和 Frame
:
的 Content
<Window x:Class="WpfApplication1.Inventory"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="Inventory" Height="300" Width="300">
<local:UserControl1 />
</Window>
Frame frame = new Frame();
frame.Content = new UserControl1();
使用 tabControl 的最佳方式是使用用户控件:示例如下:
XAML
<TabControl >
<TabItem x:Name="tab1" Header="UserControl"></TabItem>
<TabItem x:Name="tab2" Header="noControl"></TabItem>
</TabControl>
在 class 后面的代码中:
tab1.Content = new UserControl1();
然后添加名称为 UserControl1 的新用户控件:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Viewbox>
<TextBlock Text="User Control"/>
</Viewbox>
</Grid>
</UserControl>
我的 MainWindow.xaml 的结构如下
<Window>
<Grid>
<TabControl Name="MainTabCntrl">
<TabItem1>
<TabItem2>
<TabItem3>
.
.
.
<TabItemN>
</TabControl>
</Grid>
</Window>
问题是我的MainWindow.xaml目前大约有4000行代码,效率不高(你同意吗?) 我要实现的解决方案是单独创建 N windows(代表我的 TabItems),每次用户单击 Tab 项时,我都会在该 TabItem 中加载相关的 windows,如下所示
private void inventory_start()//this function is called in my MainWinodw.xaml.cs
{
inv = new Inventory(db, logViewer);//this is a window
TabItem tbItem = new TabItem();
Frame frame = new Frame();
frame.Content = inv;
tbItem.Name = "invTab";
tbItem.Content = frame;
tbItem.IsSelected = true;
MainTabCntrl.Items.Add(tbItem);
inv.swithInventoryTabs("inv_info");
}
我现在有一个错误,“'Management_V0.Inventory' 根元素对于导航无效。”
A window 不能 是另一个元素的子元素。期间.
但是您可以将 Inventory
window 的内容移动到 UserControl
(例如,只需复制并粘贴 XAML 和其中的代码文件到另一个)并将这个文件用作 Inventory
window 和 Frame
:
Content
<Window x:Class="WpfApplication1.Inventory"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="Inventory" Height="300" Width="300">
<local:UserControl1 />
</Window>
Frame frame = new Frame();
frame.Content = new UserControl1();
使用 tabControl 的最佳方式是使用用户控件:示例如下: XAML
<TabControl >
<TabItem x:Name="tab1" Header="UserControl"></TabItem>
<TabItem x:Name="tab2" Header="noControl"></TabItem>
</TabControl>
在 class 后面的代码中:
tab1.Content = new UserControl1();
然后添加名称为 UserControl1 的新用户控件:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Viewbox>
<TextBlock Text="User Control"/>
</Viewbox>
</Grid>
</UserControl>