如何使用从.xaml中的TabControl继承的类?

潜艇X

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

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

奥姆里·布蒂安(Omri Btian)

首先,修复您的字段以正确的顺序对其进行初始化

    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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在CppUnitTestFramework中对具有TEST_CLASS的类使用继承

来自分类Dev

如何使用XAML在DataContext中设置类?

来自分类Dev

使用继承XAML内部的System.Windows.Controls.Control的类

来自分类Dev

如何从一个类中访问TabControl?

来自分类Dev

如何在WPF / XAML中反映类的继承?

来自分类Dev

XAML中如何使用CollectionViewSource?

来自分类Dev

如何使用Astyanax保存继承的类?

来自分类Dev

如何对继承的类使用依赖注入?

来自分类Dev

如何在继承的类中自动声明类实例变量

来自分类Dev

如何对元类使用多重继承?

来自分类Dev

使用继承方法后,如何在使用继承方法的类中重置或替换函数?

来自分类Dev

如何使用继承的类方法并维护子类

来自分类Dev

如何在WPF / XAML中反映类的继承?

来自分类Dev

如何通过使用继承对超类中的属性进行子类化?

来自分类Dev

XAML中如何使用CollectionViewSource?

来自分类Dev

Python类继承:如何使用不在父类中的值初始化子类

来自分类Dev

如何继承CSS中的类?

来自分类Dev

在C#中,如何获得类与基类的继承距离?

来自分类Dev

如何使用反射从多层继承的类中获取受保护的朋友的财产?

来自分类Dev

如何使用继承从C#中的另一个类进行调用和对象

来自分类Dev

如何从WPF控件继承并在C#中同时使用泛型类?

来自分类Dev

如何使用继承在作为类成员的结构中添加新条目

来自分类Dev

如何使用 Mapster 映射继承的类

来自分类Dev

如何通过 XAML 在 WPF TabControl 中添加控件

来自分类Dev

如何覆盖继承类的扩展中的函数?

来自分类Dev

使用类字段在 xaml 中设置值

来自分类Dev

如何从基础 xaml“继承”

来自分类Dev

如何从应用程序中创建和使用的类库中为 TabControl.ItemsSource 创建集合?

来自分类Dev

如何重载继承类中的方法以便基类看到继承的版本?

Related 相关文章

  1. 1

    如何在CppUnitTestFramework中对具有TEST_CLASS的类使用继承

  2. 2

    如何使用XAML在DataContext中设置类?

  3. 3

    使用继承XAML内部的System.Windows.Controls.Control的类

  4. 4

    如何从一个类中访问TabControl?

  5. 5

    如何在WPF / XAML中反映类的继承?

  6. 6

    XAML中如何使用CollectionViewSource?

  7. 7

    如何使用Astyanax保存继承的类?

  8. 8

    如何对继承的类使用依赖注入?

  9. 9

    如何在继承的类中自动声明类实例变量

  10. 10

    如何对元类使用多重继承?

  11. 11

    使用继承方法后,如何在使用继承方法的类中重置或替换函数?

  12. 12

    如何使用继承的类方法并维护子类

  13. 13

    如何在WPF / XAML中反映类的继承?

  14. 14

    如何通过使用继承对超类中的属性进行子类化?

  15. 15

    XAML中如何使用CollectionViewSource?

  16. 16

    Python类继承:如何使用不在父类中的值初始化子类

  17. 17

    如何继承CSS中的类?

  18. 18

    在C#中,如何获得类与基类的继承距离?

  19. 19

    如何使用反射从多层继承的类中获取受保护的朋友的财产?

  20. 20

    如何使用继承从C#中的另一个类进行调用和对象

  21. 21

    如何从WPF控件继承并在C#中同时使用泛型类?

  22. 22

    如何使用继承在作为类成员的结构中添加新条目

  23. 23

    如何使用 Mapster 映射继承的类

  24. 24

    如何通过 XAML 在 WPF TabControl 中添加控件

  25. 25

    如何覆盖继承类的扩展中的函数?

  26. 26

    使用类字段在 xaml 中设置值

  27. 27

    如何从基础 xaml“继承”

  28. 28

    如何从应用程序中创建和使用的类库中为 TabControl.ItemsSource 创建集合?

  29. 29

    如何重载继承类中的方法以便基类看到继承的版本?

热门标签

归档