我目前遇到以下问题:WPF TabControl 折叠选项卡标题未完全隐藏(当我折叠 TabItems 时,我仍然看到 TabControl 标题所在位置有一条由几个像素组成的灰线)
但我不能在 XAML 中做到这一点(我认为),因为只有当应用程序进入全屏时我才需要隐藏 TabPanel,所以当应用程序退出全屏时,TabPanel 应该恢复可见。
我对样式经验较少,请问如何在代码中将 TabControl 的 TabPanel 设置为 Visibility.Collapsed ?
感谢您的时间和帮助,非常感谢!
如果我正确理解你需要什么,那么尝试这个实现:
<Window x:Class="Core2023.TabsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TabsWindow" Height="450" Width="800">
<Grid>
<TabControl x:Name="tc">
<TabItem Header="First">
<Border Background="Aqua"/>
</TabItem>
<TabItem Header="Second">
<Border Background="Yellow"/>
</TabItem>
</TabControl>
<CheckBox Content="TabPanel Is Visibile"
IsChecked="True" HorizontalAlignment="Center" VerticalAlignment="Center"
Click="OnIsVisibile"/>
</Grid>
</Window>
private void OnIsVisibile(object sender, RoutedEventArgs e)
{
CheckBox cb = (CheckBox)sender;
Panel panel = tc.GetVisualChild<TabPanel>();
panel.Visibility = cb.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
}
public static T GetVisualChild<T>(this DependencyObject parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}