我想在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。
但是现在,我不知道如何使用它并设置样式?
首先,修复您的字段以正确的顺序对其进行初始化
private static readonly DependencyPropertyKey ExtraHeaderPropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeader", typeof(object), typeof(TabControl), new FrameworkPropertyMetadata(null));
private static readonly DependencyPropertyKey ExtraHeaderStringFormatPropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeaderStringFormat", typeof(string), typeof(TabControl), new FrameworkPropertyMetadata(null));
private static readonly DependencyPropertyKey ExtraHeaderTemplatePropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeaderTemplate", typeof(DataTemplate), typeof(TabControl), new FrameworkPropertyMetadata(null));
private static readonly DependencyPropertyKey ExtraHeaderTemplateSelectorPropertyKey = DependencyProperty.RegisterReadOnly("ExtraHeaderTemplateSelector", typeof(DataTemplateSelector), typeof(TabControl), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty ExtraHeaderProperty = ExtraHeaderPropertyKey.DependencyProperty;
public static readonly DependencyProperty ExtraHeaderStringFormatProperty = ExtraHeaderStringFormatPropertyKey.DependencyProperty;
public static readonly DependencyProperty ExtraHeaderTemplateProperty = ExtraHeaderTemplatePropertyKey.DependencyProperty;
public static readonly DependencyProperty ExtraHeaderTemplateSelectorProperty = ExtraHeaderTemplateSelectorPropertyKey.DependencyProperty;
xmlns
使用类名称psace在窗口中添加一个声明
xmlns:local="clr-namespace:tabcontrol"
并使用您的班级而不是TabControl
班级
<local:MyTabControl ....>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句