隐藏 TabControl 标头

发布于 2024-08-05 08:10:11 字数 172 浏览 7 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

娇妻 2024-08-12 08:10:11

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

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

压抑⊿情绪 2024-08-12 08:10:11
Style s = new Style();
s.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
tabControl.ItemContainerStyle = s;
Style s = new Style();
s.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
tabControl.ItemContainerStyle = s;
汐鸠 2024-08-12 08:10:11

简单的XAML风格

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

Simple XAML Style

<TabControl>
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
    </TabControl.ItemContainerStyle>
    ...
</TabControl>
浅浅 2024-08-12 08:10:11

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

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

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

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#

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));
      }
    }
  }
}

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...

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

<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#

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));
      }
    }
  }
}
嘴硬脾气大 2024-08-12 08:10:11

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

tabItemToAdd.Visibility = Visibility.Collapsed;

...但是后来发生了一件奇怪的事情,第二次我清除选项卡控件的项目,再次创建选项卡项目并使用这种方法在将其添加到选项卡控件之前,整个选项卡项及其内容都消失了,而不仅仅是选项卡标题。因此,我在相当于 此解决方案

tabItemToAdd.Template = new ControlTemplate();

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();
话少心凉 2024-08-12 08:10:11

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

tabItemName.Visibility = Visibility.Collapsed;

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

tabItemName.Visibility = Visibility.Collapsed;
北渚 2024-08-12 08:10:11
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;
    }
}
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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文