c# - 以编程方式将带有自定义控件的网格添加到 WPF TabControl 不会出现
问题描述
我正在开发 WPF 应用程序,并尝试以编程方式将自定义控件作为 TabItems 添加到 TabControl。在将网格作为内容添加到 TabItem 之前,我创建了一个网格并将自定义控件放置在网格内。这是我在 XAML 中定义的内容:
<TabControl x:Name="studentTabControl" Margin="5">
<TabControl.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid x:Name="Panel">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="Gold"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Panel" Property="Background" Value="LightGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem x:Name="student1TabItem" Tag="1" Visibility="Visible">
<TabItem.Header>
<TextBlock Text="Student #1" FontSize="14" FontWeight="Bold" />
</TabItem.Header>
</TabItem>
<TabItem x:Name="student2TabItem" Tag="2" Visibility="Visible">
<TabItem.Header>
<TextBlock Text="Student #2" FontSize="14" FontWeight="Bold" />
</TabItem.Header>
</TabItem>
<TabItem x:Name="student3TabItem" Tag="3" Visibility="Visible">
<TabItem.Header>
<TextBlock Text="Student #3" FontSize="14" FontWeight="Bold" />
</TabItem.Header>
</TabItem>
<TabItem x:Name="student4TabItem" Tag="4" Visibility="Visible">
<TabItem.Header>
<TextBlock Text="Student #4" FontSize="14" FontWeight="Bold" />
</TabItem.Header>
</TabItem>
<TabItem x:Name="student5TabItem" Tag="5" Visibility="Visible">
<TabItem.Header>
<TextBlock Text="Student #5" FontSize="14" FontWeight="Bold" />
</TabItem.Header>
</TabItem>
<TabItem x:Name="student6TabItem" Tag="6" Visibility="Visible">
<TabItem.Header>
<TextBlock Text="Student #6" FontSize="14" FontWeight="Bold" />
</TabItem.Header>
</TabItem>
</TabControl>
我将 Visibility 显式设置为 Visible 以进行调试。我计划折叠所有在运行时不可见的 TabItems,并在需要特定选项卡时使每个 TabItems 可见。选项卡应该根据运行时要求来来去去。
这是将创建网格和自定义控件并将它们放置在 TabItems 中的代码。
foreach (var tab in studentTabs.Select((x, i) => new { Item = x, Index = i }))
{
string gridName = "student" + (tab.Index + 1).ToString() + "Grid";
string tabName = "student" + (tab.Index + 1).ToString() + "Tab";
var tabitem = (TabItem)this.FindName(tabName + "Item");
//var grid = (Grid)this.FindName(gridName);
// if (grid != null && tabitem != null)
if (tabitem != null)
{
Grid grid = new Grid()
{
Name = gridName,
Background = new SolidColorBrush(Colors.Gray)
};
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
Controls.StudentTabControl tabControl = new Controls.StudentTabControl()
{
Name = tabName + "Control",
TabStudentNumber = (tab.Index + 1).ToString(),
HorizontalAlignment = HorizontalAlignment.Center,
Width = 1180,
Height = 1120,
Margin = new Thickness(5),
VerticalAlignment = VerticalAlignment.Center
};
grid.Children.Add(tabControl);
Grid.SetColumn(tabControl, 0);
Grid.SetRow(tabControl, 0);
tabitem.Content = grid;
studentTabs[tab.Index] = tabControl;
}
this.studentTabControl.UpdateLayout();
}
自定义控件非常复杂,但它本质上是从外部 Grid 开始的,并且所有内容都放在 XAML 中。在运行时,我查看了 Visual Tree,但没有看到添加的 Grid 和自定义控件。我不确定发生了什么以及为什么我会从树中丢失添加的控件。我只是没有看到问题。
顺便说一句,我首先在 XAML 的 TabItems 中预定义了 Grid,并尝试创建自定义控件并以编程方式添加它,但这不起作用。但是,我确实在 Visual Tree 中看到了网格。
最后,我应该提一下,我添加所有内容的窗口不是主应用程序,它是一个单独的 Window 类型的弹出窗口。在弹出窗口上调用 Show() 之前,我会初始化并创建所有内容。
任何可以提供的信息都会有很大帮助。
解决方案
推荐阅读
- javascript - 过滤器不适用于第三个子级别(嵌套级别)
- c++ - 如何从控制台启动可执行文件并使用 Qt 读取输出
- python - 如何使用python遍历二进制块并在文件末尾停止?
- php - Laravel 5.4 - 如何使用 EDI 供应商库
- angular - Angular 7+,从父组件调用子组件的方法好吗?
- azure - 将资源组从开发公司转移到客户公司
- java - 如何为 selenium +TestNG+java 项目设置魅力?
- javascript - 将 jQuery 搜索限制为特定容器,例如 h 和 p 标签
- javascript - 下拉固定位置和格式不起作用
- android - Play 商店报告“您的设备与此版本不兼容”但它通过 adb 安装在 onePlus 上就好了