我想在 TabControl 的顶部添加一个“新”按钮,如下所示:
所以,我创建了一个继承自 TabControl 的类 MyTabControl:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
namespace tabcontrol
{
[TemplatePart(Name="PART_ExtraHeaderHost", Type=typeof(ContentPresenter))]
public class MyTabControl : TabControl
{
#region Fields
private const string ExtraHeaderHostTemplateName = "PART_ExtraHeaderHost";
public static readonly DependencyProperty ExtraHeaderProperty = ExtraHeaderPropertyKey.DependencyProperty;
private static readonly DependencyPropertyKey ExtraHeaderPropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeader", typeof(object), typeof(TabControl), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty ExtraHeaderStringFormatProperty = ExtraHeaderStringFormatPropertyKey.DependencyProperty;
private static readonly DependencyPropertyKey ExtraHeaderStringFormatPropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeaderStringFormat", typeof(string), typeof(TabControl), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty ExtraHeaderTemplateProperty = ExtraHeaderTemplatePropertyKey.DependencyProperty;
private static readonly DependencyPropertyKey ExtraHeaderTemplatePropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeaderTemplate", typeof(DataTemplate), typeof(TabControl), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty ExtraHeaderTemplateSelectorProperty = ExtraHeaderTemplateSelectorPropertyKey.DependencyProperty;
private static readonly DependencyPropertyKey ExtraHeaderTemplateSelectorPropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeaderTemplateSelector", typeof(DataTemplateSelector), typeof(TabControl), new FrameworkPropertyMetadata(null));
#endregion Fields
#region Properties
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public object ExtraHeader
{
get
{
return base.GetValue(ExtraHeaderProperty);
}
internal set
{
base.SetValue(ExtraHeaderPropertyKey, value);
}
}
internal ContentPresenter ExtraHeaderPresenter
{
get
{
return (base.GetTemplateChild("PART_ExtraHeaderHost") as ContentPresenter);
}
}
public string ExtraHeaderStringFormat
{
get
{
return (string)base.GetValue(ExtraHeaderStringFormatProperty);
}
internal set
{
base.SetValue(ExtraHeaderStringFormatPropertyKey, value);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public DataTemplate ExtraHeaderTemplate
{
get
{
return (DataTemplate)base.GetValue(ExtraHeaderTemplateProperty);
}
internal set
{
base.SetValue(ExtraHeaderTemplatePropertyKey, value);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public DataTemplateSelector ExtraHeaderTemplateSelector
{
get
{
return (DataTemplateSelector)base.GetValue(ExtraHeaderTemplateSelectorProperty);
}
internal set
{
base.SetValue(ExtraHeaderTemplateSelectorPropertyKey, value);
}
}
#endregion Properties
}
}
PART_ExtraHeaderHost
放置“新”按钮。考虑到以后放置其他控件,所以使用PART_ExtraHeaderHost
而不是Button。
但是现在,我不知道如何使用它和样式它?