using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
public partial class MainWindow : Window
{
TabControl tabControl;
Style styleTabPanel;
public MainWindow()
{
InitializeComponent();
CheckBox chk = new CheckBox();
{
chk.Margin = new Thickness(5);
chk.Content = "Test";
chk.Checked += Chk_Checked;
chk.Unchecked += Chk_Checked;
}
TabItem item = new TabItem() { Header = "Tab", Content = chk };
tabControl = new TabControl();
tabControl.RenderTransform = new ScaleTransform(5, 5);
tabControl.Items.Add(item);
this.Content = tabControl;
}
private void Chk_Checked(object sender, RoutedEventArgs e)
{
var chk = (CheckBox)sender;
if (chk.IsChecked == true)
{
if (styleTabPanel == null)
{
styleTabPanel = new Style(typeof(System.Windows.Controls.Primitives.TabPanel));
styleTabPanel.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
}
tabControl.Resources.Add(styleTabPanel.TargetType, styleTabPanel);
}
else
{
tabControl.Resources.Remove(styleTabPanel.TargetType);
}
}
}
Change TabPanel to Visibility.Collapsed in Code-Behind
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!
Developer technologies | Windows Presentation Foundation
Developer technologies | C#
-
gekka 12,206 Reputation points MVP Volunteer Moderator
2023-12-27T13:21:56.2533333+00:00
1 additional answer
Sort by: Most helpful
-
Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
2023-12-28T03:37:07.4633333+00:00 Hi,@Dyr Fenrir . Welcome to Microsoft Q&A .
In that case, you could use the
Window_StateChanged
event in the code-behind to handle the state changes and update the visibility of yourTabPanel
. Xaml:<Window x:Class="TabPanelDemo.MainWindow" ... Name="window" StateChanged="Window_StateChanged"> <TabControl x:Name="tabControl" Style="{DynamicResource TabControlStyle1}" > <TabItem >tab1</TabItem> </TabControl> </Window>
Codebehind:
private void Window_StateChanged(object sender, EventArgs e) { if (WindowState == WindowState.Maximized) { SetTabPanelVisibility(false); } else { SetTabPanelVisibility(true); } } private void SetTabPanelVisibility(bool isVisible) { var tabPanel = FindVisualChild<TabPanel>(tabControl); if (tabPanel != null) { tabPanel.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed; } } private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (child is T result) { return result; } T childResult = FindVisualChild<T>(child); if (childResult != null) { return childResult; } } return null; }
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.