[英]WPF custom TabItem disappears upon restyle?
我有一個帶有自定義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
行,它將顯示。
無論如何,我可能都將這一切設置錯了。 最終目標是要關閉標簽頁。
摘抄:
首先,如果您不熟悉主題樣式與顯式樣式的概念,建議您閱讀此博客http://www.interact-sw.co.uk/iangblog/2007/02/14/wpfdefaulttemplate 。 所有控件都需要主題樣式(通常在主題文件夾下的Aero.NormalColor.xaml / Generic.xaml / etc文件中定義),該主題樣式定義其默認外觀(模板)/屬性和可選的顯式樣式(已定義)在元素/窗口/應用程序級別使用隱式鍵或顯式鍵)。
DefaultStyleKeyProperty定義用於查找控件主題樣式的鍵。 如果注釋掉該行,您將得到基類的默認主題樣式。 作為一項快速測試,將自定義控件的基類更改為Button。 如果注釋掉該行,則您的自定義控件將獲得Button的主題樣式,它看起來像一個按鈕。 如果您不注釋掉該行,則您的自定義控件將獲得generic.xaml中定義的默認樣式。
來自: http : //social.msdn.microsoft.com/Forums/zh-CN/wpf/thread/9df46c62-3715-4e16-8ef6-538068c28eb6/
編輯:
有什么理由為什么您不想定義標題模板並分配它?
例如 <TabItem Header="Testing" HeaderTemplate="{StaticResource customHeaderTemplate}" />
正如Schalk所指出的,DefaultStyleKeyProperty標識默認樣式的鍵,該鍵必須包含在Themes文件夾下的Resource字典中。 因此,如果將CustomTabItem.xaml
移動/重命名為Themes\\Generic.xaml
,則應加載並應用它。
您還可以根據系統主題創建不同版本的樣式,方法是將名為AeroNormalColor.xaml,Classic.xaml等的文件添加到Themes文件夾中。 這些文件還需要具有樣式的副本,大概是通過視覺調整來實現的。
您可以從此處下載本機控件的默認樣式。 這將允許您復制TabItem的默認樣式,並根據需要進行調整。
或者,您可以在此問題中執行類似的操作,其中將關閉按鈕添加到HeaderTemplate中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.