0

我想在 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。

但是现在,我不知道如何使用它和样式它?

4

1 回答 1

0

首先,修复你的字段以正确的顺序初始化它们

    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声明

 xmlns:local="clr-namespace:tabcontrol"

并使用您的班级而不是TabControl班级

<local:MyTabControl ....>
于 2013-09-25T05:27:14.043 回答