样式 TabItems,不影响嵌套的 TabControls [英] Style TabItems, don't affect nested TabControls
本文介绍了样式 TabItems,不影响嵌套的 TabControls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为 TabControl 的 TabItems 设置样式.问题是,样式会影响嵌套 TabControl 的项目.传播样式是我所知道的获取 TabItems 的唯一方法.任何人都知道如何在外部 TabControl 上设置 TabItems 的样式?
就我而言,内部标签是在插件中定义的,因此我无法访问它们以尝试
MainWindow.xaml
<网格><TabControl ItemsSource="{Binding}"><TabControl.Resources><Style TargetType="TabItem"><Setter 属性="模板"><Setter.Value><ControlTemplate TargetType="TabItem"><Border Name="Border" Background="Red"><ContentPresenter ContentSource="Header"/></边框></控制模板></Setter.Value></Setter></风格></TabControl.Resources><TabControl.ItemTemplate><数据模板><TextBlock Text="{Binding TabName}"/></数据模板></TabControl.ItemTemplate><TabControl.ContentTemplate><数据模板><ContentControl Content="{Binding TabConent}"/></数据模板></TabControl.ContentTemplate></TabControl></网格></窗口>
MainWindow.xaml.cs
使用 System.Windows;使用 System.Windows.Controls;使用 System.Collections.ObjectModel;命名空间 WpfApplication1{公共部分类 MainWindow : 窗口{公共类 TabData{公共字符串 TabName { 获取;放;}公共标签 TabConent{得到{//在实际情况下,这个 TabControl 来自别人的插件var content = new TabControl();content.Items.Add(new TabItem() { Header = "Nested Tab Item" });返回新标签(){内容=内容};}}}公共主窗口(){DataContext = new ObservableCollection() { new TabData() { TabName = "Tab Item" } };;初始化组件();}}}
解决方案
使用这个,看看这是否是你想要的:
<Style TargetType="TabItem"><Style.Triggers><DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl, AncestorLevel=2}}" Value="{x:Null}"><Setter 属性="模板"><Setter.Value><ControlTemplate TargetType="TabItem"><Border Name="Border" Background="Red"><ContentPresenter ContentSource="Header"/></边框></控制模板></Setter.Value></Setter></数据触发器></Style.Triggers></风格></TabControl.Resources>
I'm styling TabItems of a TabControl. The problem is, the style affects items of nested TabControls. Propagating styles are the only way I know to get to the TabItems. Anyone know how to style the TabItems on just the outer TabControl?
In my case, the inner tabs are defined in plugins, so I can't access them to try this answer.
Demo app
Here's a demo app of my situation.
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl ItemsSource="{Binding}">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Border Name="Border" Background="Red">
<ContentPresenter ContentSource="Header"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TabName}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding TabConent}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public class TabData
{
public string TabName { get; set; }
public Label TabConent
{
get
{
// In real case, this TabControl from someone else's plugin
var content = new TabControl();
content.Items.Add(new TabItem() { Header = "Nested Tab Item" });
return new Label() { Content = content };
}
}
}
public MainWindow()
{
DataContext = new ObservableCollection<TabData>() { new TabData() { TabName = "Tab Item" } }; ;
InitializeComponent();
}
}
}
解决方案
Use this, and tell if this is what you want :
<TabControl.Resources>
<Style TargetType="TabItem">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl, AncestorLevel=2}}" Value="{x:Null}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Border Name="Border" Background="Red">
<ContentPresenter ContentSource="Header"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TabControl.Resources>
这篇关于样式 TabItems,不影响嵌套的 TabControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文