Change TabPanel to Visibility.Collapsed in Code-Behind

47 Views Asked by At

I am currently experiencing the exact following issue: WPF TabControl collapsed tab headers are not completely hidden (when I collapse the TabItems, I still see a gray line of a few pixels where the TabControl header is located)

But I cannot do it in XAML (I think), because I need to hide the TabPanel only when the application goes into full screen, so when the application exits full screen, the TabPanel should go back to visible.

I am less experienced in styles, how would I set the TabPanel of my TabControl to Visibility.Collapsed in code please?

Thank you for your time and help, it is greatly appreciated!

1

There are 1 best solutions below

0
EldHasp On

If I understand correctly what you need, then try this implementation:

<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.GetFirstChild<TabPanel>();
            panel.Visibility = cb.IsChecked == true
                               ? Visibility.Visible
                               : Visibility.Collapsed;
        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using static System.Windows.Media.VisualTreeHelper;


namespace CommonCore.Helpers
{
    public static partial class VisualTreeHelper
    {
        public static IEnumerable<DependencyObject> GetChildren(this DependencyObject parent)
        {
            ArgumentNullException.ThrowIfNull(parent);
            Queue<DependencyObject> queue = new Queue<DependencyObject>(16);
            queue.Enqueue(parent);
            while (queue.Count != 0)
            {
                DependencyObject current = queue.Dequeue();
                yield return current;

                int count = GetChildrenCount(current);
                for (int i = 0; i < count; i++)
                {
                    queue.Enqueue(GetChild(current, i));
                }
            }
        }
        public static IEnumerable<T> GetChildren<T>(this DependencyObject parent)
           where T : DependencyObject
            => parent.GetChildren().OfType<T>();

        public static T GetFirstChild<T>(this DependencyObject parent)
           where T : DependencyObject
            => (T)parent.GetChildren().FirstOrDefault(child => child is T);

    }