首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF UserControl如何继承WPF UserControl?

WPF UserControl如何继承WPF UserControl?
EN

Stack Overflow用户
提问于 2009-05-20 12:01:20
回答 5查看 64.9K关注 0票数 99

下面的WPF UserControl调用了DataTypeWholeNumber,它可以工作。

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

许多依赖属性将由所有这些控件共享,因此我希望将它们的公共方法放到一个BaseDataType中,并让这些UserControls中的每一个都继承这个基类型。

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

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

代码语言:javascript
复制
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());



    }
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-05-20 12:03:09

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

所以

代码语言:javascript
复制
<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"
    >

变成了

代码语言:javascript
复制
<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"
    >

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

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

Stack Overflow用户

发布于 2014-01-31 17:07:56

代码语言:javascript
复制
public partial class MooringConfigurator : MooringLineConfigurator
    {
        public MooringConfigurator()
        {
            InitializeComponent();
        }
    }



<dst:MooringLineConfigurator x:Class="Wave.Dashboards.Instruments.ConfiguratorViews.DST.MooringConfigurator"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:dst="clr-namespace:Wave.Dashboards.Instruments.ConfiguratorViews.DST"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</dst:MooringLineConfigurator>    
票数 2
EN

Stack Overflow用户

发布于 2012-01-31 05:52:34

我在这篇文章中找到了答案:http://www.paulstovell.com/xmlnsdefinition

基本上,这意味着您应该在AssemlyInfo.cs文件中定义一个XML名称空间,该名称空间可以在XAML中使用。它对我有效,但是我将基用户控件类放在了一个单独的DLL中……

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/887519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档