C# 隐藏 TabControl 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1465443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:49:46  来源:igfitidea点击:

Hide the TabControl header

c#wpftabstabcontrol

提问by Elazar Leibovich

What's the programmatic way (ie not using styles as in this question, but using code) to hide the TabControlheader? I'll be glad for a snippet.

隐藏标题的编程方式是什么(即不使用本问题中的样式,而是使用代码)TabControl?我会很高兴的一个片段。

采纳答案by Thomas Levesque

Style s = new Style();
s.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
tabControl.ItemContainerStyle = s;

回答by Anvaka

Well, there are several ways to do this.

嗯,有几种方法可以做到这一点。

The ugliest way: Use VisualTreeHelper to find TabPanel (or any other Panel you use to host items), and set it's Visibility property to Visibility.Collapsed. Why ugly? It's easy to create few annoying bugs here or break this approach with 'harmless' style update if you was not careful enough...

最丑陋的方法:使用 VisualTreeHelper 查找 TabPanel(或用于托管项目的任何其他面板),并将其 Visibility 属性设置为 Visibility.Collapsed。为什么丑?如果您不够小心,很容易在这里创建一些烦人的错误或通过“无害”样式更新破坏这种方法......

I prefer using combination of Xaml and code behind. You bind either TabItem's visibility to view model property or TabPanel's visibility to view model property. In both cases you have to override style (either ItemContainer's style or whole TabControl's style). In both cases you have view model. Now, to toggle tab header's visibility, you just update a property in the view model. Here is an example with TabItems:

我更喜欢使用 Xaml 和代码背后的组合。您将 TabItem 的可见性绑定到视图模型属性,或将 TabPanel 的可见性绑定到视图模型属性。在这两种情况下,您都必须覆盖样式(ItemContainer 的样式或整个 TabControl 的样式)。在这两种情况下,您都有视图模型。现在,要切换选项卡标题的可见性,您只需更新视图模型中的属性。以下是 TabItems 的示例:

XAML

XAML

<Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="Tab Settings"
        Height="300"
        Width="300">
  <Window.Resources>
    <local:TabControlViewModel x:Key="tabVM" />
    <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
  </Window.Resources>
  <Grid>
    <TabControl DataContext="{StaticResource tabVM}">
      <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
          <Setter Property="Visibility"
                  Value="{Binding TabHeaderVisible, Converter={StaticResource booleanToVisibilityConverter}}" />
        </Style>
      </TabControl.ItemContainerStyle>
      <TabItem Header="Tab 1">
        <StackPanel>
          <TextBlock Text="Content" />
          <Button Content="Toggle Header"
                  Click="ToggleHeaderClick" />
        </StackPanel>
      </TabItem>
      <TabItem Header="Tab 2 Header">
        <TextBlock Text="Tab 2 Content" />
      </TabItem>
    </TabControl>
  </Grid>
</Window>

C#

C#

using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication5
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    private void ToggleHeaderClick(object sender, RoutedEventArgs e)
    {
      var tabControlVM =
        ((FrameworkElement)sender).DataContext as TabControlViewModel;
      if (tabControlVM != null)
      {
        tabControlVM.TabHeaderVisible = !tabControlVM.TabHeaderVisible;
      }
    }
  }

  public class TabControlViewModel : INotifyPropertyChanged
  {
    private bool _tabHeaderVisible = true;

    public ICommand ToggleHeader
    {
      get; private set;
    }

    public bool TabHeaderVisible
    {
      get { return _tabHeaderVisible; }
      set
      {
        _tabHeaderVisible = value;
        OnPropertyChanged("TabHeaderVisible");
      }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string name)
    {
      var changed = PropertyChanged;
      if (changed != null)
      {
        changed(this, new PropertyChangedEventArgs(name));
      }
    }
  }
}

回答by EightyOne Unite

Actually, it's very straight forward to hide the tab strip. You just set each TabItems Visibilityto Collapsed. You still see the tab content,...just not the tab header itself.

实际上,隐藏标签条非常简单。您只需将每个TabItems设置VisibilityCollapsed. 您仍然可以看到选项卡内容,...只是不是选项卡标题本身。

回答by user3549063

private void TabItemControl_MouseEnter(object sender, MouseEventArgs e)
{
    if (this.TabItemControl.IsSelected == false)
    {
        this.TabItemControl.Opacity = 100;
    }
}

private void TabItemControl_MouseLeave(object sender, MouseEventArgs e)
{
    if (this.TabItemControl.IsSelected == false)
    {
        this.TabItemControl.Opacity = 0;
    }
}

private void TabAllControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (this.TabItemControl.IsSelected == false)
    {
        this.TabItemControl.Opacity = 0;
    }
}

回答by Sebastian

Simple XAML Style

简单的 XAML 样式

<TabControl>
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
    </TabControl.ItemContainerStyle>
    ...
</TabControl>

回答by Eric Eggers

I've tried this in some code where I populate the tab items manually...

我已经在一些代码中尝试过这个,我手动填充了选项卡项目......

tabItemToAdd.Visibility = Visibility.Collapsed;

...but then I had a weird thing happen where the second time I'd clear out the tab control's items, create the tab item again and use this approach before adding it to the tab control, the entire tab item and its contents were gone, not just the tab header. So I've had success with the programmatic equivalent of this solution:

...但后来我发生了一件奇怪的事情,我第二次清除选项卡控件的项目,再次创建选项卡项目并在将其添加到选项卡控件之前使用这种方法,整个选项卡项目及其内容是不见了,不仅仅是标签页眉。所以我在这个解决方案的程序化方面取得了成功:

tabItemToAdd.Template = new ControlTemplate();

回答by Ronnie

If you use C# and set the x:Name for the TabItem, you can also manipulate the Visibility like so:

如果您使用 C# 并为 TabItem 设置 x:Name,您还可以像这样操作 Visibility:

tabItemName.Visibility = Visibility.Collapsed;