WPF UserControl如何继承WPF UserControl?

Edw*_*uay 87 c# wpf xaml user-controls

以下WPF UserControl称为DataTypeWholeNumber,它可以正常工作.

现在我想创建一个名为DataTypeDateTimeDataTypeEmail等的UserControl .

许多依赖属性将由所有这些控件共享,因此我想将它们的公共方法放入BaseDataType中,并使每个UserControl都继承自此基类型.

但是,当我这样做时,我得到错误:部分声明可能没有不同的基类.

那么如何使用UserControls实现继承,以便共享功能全部在基类中?

using System.Windows;
using System.Windows.Controls;

namespace TestDependencyProperty827.DataTypes
{
    public partial class DataTypeWholeNumber : BaseDataType
    {
        public DataTypeWholeNumber()
        {
            InitializeComponent();
            DataContext = this;

            //defaults
            TheWidth = 200;
        }

        public string TheLabel
        {
            get
            {
                return (string)GetValue(TheLabelProperty);
            }
            set
            {
                SetValue(TheLabelProperty, value);
            }
        }

        public static readonly DependencyProperty TheLabelProperty =
            DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType),
            new FrameworkPropertyMetadata());


        public string TheContent
        {
            get
            {
                return (string)GetValue(TheContentProperty);
            }
            set
            {
                SetValue(TheContentProperty, value);
            }
        }

        public static readonly DependencyProperty TheContentProperty =
            DependencyProperty.Register("TheContent", typeof(string), typeof(BaseDataType),
            new FrameworkPropertyMetadata());


        public int TheWidth
        {
            get
            {
                return (int)GetValue(TheWidthProperty);
            }
            set
            {
                SetValue(TheWidthProperty, value);
            }
        }

        public static readonly DependencyProperty TheWidthProperty =
            DependencyProperty.Register("TheWidth", typeof(int), typeof(DataTypeWholeNumber),
            new FrameworkPropertyMetadata());



    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ris 122

确保您已更改xaml中的第一个标记以继承新的基本类型

所以

<UserControl x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    >
Run Code Online (Sandbox Code Playgroud)

<myTypes:BaseDataType x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    xmlns:myTypes="clr-namespace:TestDependencyProperty827.DataTypes"
    >
Run Code Online (Sandbox Code Playgroud)

因此,总结完整的答案,包括以下评论中的额外细节:

  • 基类不应包含xaml文件.在单个(非部分)cs文件中定义它并将其定义为直接从Usercontrol继承.
  • 确保子类在cs代码隐藏文件和xaml的第一个标记中继承基类(如上所示).

  • 如果您可以使基类成​​为继承自UserControl的普通类型,而不定义任何xmal(因此不是分割为两个文件的部分类).问题是你不能继承xmal,所以base或child类不需要有xmal文件.或者使您的类自定义控件而不是用户控件.这样,外观在部分类之外定义,但是您将失去用户控件的易用性. (9认同)
  • 当我进行更改时,我得到错误:"... DataTypes.BaseDataType不能是XAML文件的根,因为它是使用XAML定义的",你如何摆脱这个循环引用? (4认同)