WPF 自定义 TabItem 在重新样式后消失?
我有一个带有自定义 TabItem 的 TabControl。视图 xaml 看起来像这样。
<UserControl x:Class="PeripheryModule.Views.MainTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PeripheryModule.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TabControl>
<TabItem Header="TabItem 4" />
<local:CustomTabItem Header="Testing" />
</TabControl>
</Grid>
</UserControl>
CustomTabItem.xaml 文件看起来像这样
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PeripheryModule.Controls">
<Style TargetType="{x:Type local:CustomTabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomTabItem}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
, CustomTabItem.xaml.cs 文件看起来像这样
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PeripheryModule.Controls
{
public class CustomTabItem : TabItem
{
static CustomTabItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTabItem), new FrameworkPropertyMetadata(typeof(CustomTabItem)));
}
}
}
我没有抛出任何错误,所发生的只是 tabItem 根本不显示。但是,如果我注释掉 CustomTabItem.aspx.cs 中的 DefaultStyleKeyProperty
行,它就会显示。
无论如何,我可能把这一切都设置错了。最终目标是拥有可关闭的选项卡。
I have a TabControl with a custom TabItem. The view xaml looks like this.
<UserControl x:Class="PeripheryModule.Views.MainTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PeripheryModule.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TabControl>
<TabItem Header="TabItem 4" />
<local:CustomTabItem Header="Testing" />
</TabControl>
</Grid>
</UserControl>
the CustomTabItem.xaml file looks like this
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PeripheryModule.Controls">
<Style TargetType="{x:Type local:CustomTabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomTabItem}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
and the CustomTabItem.xaml.cs file looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PeripheryModule.Controls
{
public class CustomTabItem : TabItem
{
static CustomTabItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTabItem), new FrameworkPropertyMetadata(typeof(CustomTabItem)));
}
}
}
I get no errors thrown, all that happens is the tabItem simply doesn't show up. It will, however, show up if i comment out the DefaultStyleKeyProperty
line in CustomTabItem.aspx.cs.
I've probably got this all set up wrong anyways. Ultimate goal is to have closeable tabs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
摘抄:
来自: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9df46c62-3715-4e16-8ef6-538068c28eb6/
编辑:
有什么理由不这样做吗想要定义标题模板并分配它?
前任。
Excerpt:
From: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9df46c62-3715-4e16-8ef6-538068c28eb6/
Edit:
Is there any reason why you don't want to define the header template and assign it?
ex.
<TabItem Header="Testing" HeaderTemplate="{StaticResource customHeaderTemplate}" />
正如 Schalk 指出的,DefaultStyleKeyProperty 标识默认样式的键,该键必须包含在 Themes 文件夹下的资源字典中。因此,如果您将
CustomTabItem.xaml
移动/重命名到Themes\Generic.xaml
下,那么它应该加载并应用。您还可以通过将名为 AeroNormalColor.xaml、Classic.xaml 等的文件添加到 Themes 文件夹中,根据系统主题创建不同版本的样式。这些文件还需要有您的样式的副本,大概需要进行视觉调整。
您可以从 这里。这将允许您复制 TabItem 的默认样式并根据您的需要进行调整。
或者你可以这样做 问题,其中关闭按钮被添加到 HeaderTemplate 中。
As Schalk points out, the DefaultStyleKeyProperty identifies the key to the default Style, which must be contained in a Resource dictionary under a Themes folder. So if you move/rename your
CustomTabItem.xaml
to be underThemes\Generic.xaml
, then it should load and be applied.You can also create different versions of your styles based on the system themes, by adding files named AeroNormalColor.xaml, Classic.xaml, etc to the Themes folder. These files would need to have copies of your Style also, presumably with visual tweaks.
You can download the default Styles for the native controls from here. This would allow you to copy the default Style for TabItem and tweak it to your needs.
Or you could do something like in this question, where the close button is added to the HeaderTemplate.