【发布时间】:2009-09-23 11:35:02
【问题描述】:
隐藏TabControl 标头的编程方式是什么(即不使用this question 中的样式,而是使用代码)?我会很高兴有一个sn-p。
【问题讨论】:
标签: c# wpf tabs tabcontrol
隐藏TabControl 标头的编程方式是什么(即不使用this question 中的样式,而是使用代码)?我会很高兴有一个sn-p。
【问题讨论】:
标签: c# wpf tabs tabcontrol
实际上,隐藏标签条非常简单。您只需将每个 TabItems Visibility 设置为 Collapsed。您仍然可以看到选项卡内容,...只是看不到选项卡标题本身。
【讨论】:
Style s = new Style();
s.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
tabControl.ItemContainerStyle = s;
【讨论】:
简单的 XAML 样式
<TabControl>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</TabControl.ItemContainerStyle>
...
</TabControl>
【讨论】:
嗯,有几种方法可以做到这一点。
最丑陋的方法:使用 VisualTreeHelper 找到 TabPanel(或您用来承载项目的任何其他 Panel),并将其 Visibility 属性设置为 Visibility.Collapsed。为什么丑?如果您不够小心,很容易在这里创建一些烦人的错误,或者通过“无害”样式更新来破坏这种方法......
我更喜欢结合使用 Xaml 和后面的代码。您将 TabItem 的可见性绑定到视图模型属性或将 TabPanel 的可见性绑定到视图模型属性。在这两种情况下,您都必须覆盖样式(ItemContainer 的样式或整个 TabControl 的样式)。在这两种情况下,您都有视图模型。现在,要切换选项卡标题的可见性,您只需更新视图模型中的属性。这是 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));
}
}
}
}
【讨论】:
tab.Header.Visible = false 这样的行不是一个巨大的 XML + 代码背后的怪物!
我已经在一些手动填充选项卡项的代码中尝试过这个...
tabItemToAdd.Visibility = Visibility.Collapsed;
...但是后来我发生了一件奇怪的事情,第二次我要清除选项卡控件的项目,再次创建选项卡项目并使用这种方法,然后再将其添加到选项卡控件、整个选项卡项目和它的内容不见了,而不仅仅是标签页眉。所以我在this solution 的程序等效项上取得了成功:
tabItemToAdd.Template = new ControlTemplate();
【讨论】:
如果您使用 C# 并为 TabItem 设置 x:Name,您还可以像这样操作 Visibility:
tabItemName.Visibility = Visibility.Collapsed;
【讨论】:
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;
}
}
【讨论】: