WPF UserControl 如何继承 WPF UserControl? [英] How can a WPF UserControl inherit a WPF UserControl?

查看:45
本文介绍了WPF UserControl 如何继承 WPF UserControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下名为 DataTypeWholeNumber 的 WPF UserControl 有效.

The following WPF UserControl called DataTypeWholeNumber which works.

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

Now I want to make a UserControl called DataTypeDateTime and DataTypeEmail, etc.

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

Many of the Dependency Properties will be shared by all these controls and therefore I want to put their common methods into a BaseDataType and have each of these UserControls inherit from this base type.

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

However, when I do that, I get the error: Partial Declaration may not have different base classes.

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

So how can I implement inheritance with UserControls so shared functionality is all in the base class?

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());



    }
}

推荐答案

确保您已将 xaml 中的第一个标签更改为也继承自您的新基类型

Ensure that you have changed the first tag in the xaml to also inherit from your new basetype

所以

<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"
    >

变成

<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"
    >

<小时>

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


So, to summarise the complete answer including the extra details from the comments below:

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

这篇关于WPF UserControl 如何继承 WPF UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆