WPF UserControl如何inheritanceWPF UserControl?

下面的WPF UserControl称为DataTypeWholeNumber它的作品。

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

许多依赖属性将被所有这些控件共享,因此我想将它们的通用方法放到一个BaseDataType中,并让这些UserControl中的每一个都inheritance这个基types。

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

那么我怎样才能实现与UserControl的inheritance,所以共享的function都在基类?

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中的第一个标记更改为也从新的基类inheritance

所以

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

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

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

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

基本上说的是,你应该在AssemlyInfo.cs文件中定义一个XML名字空间,这个名字空间可以在XAML中使用。 它为我工作,但我把基本的用户控制类放在一个单独的DLL …

我遇到了同样的问题,但需要控制从devise师不支持的抽象类inheritance。 什么解决了我的问题是使usercontrol从一个标准类(inheritanceUserControl)和一个接口inheritance。 这样devise师正在工作。

 //the xaml <local:EcranFiche x:Class="VLEva.SIFEval.Ecrans.UC_BatimentAgricole" xmlns:local="clr-namespace:VLEva.SIFEval.Ecrans" ...> ... </local:EcranFiche> // the usercontrol code behind public partial class UC_BatimentAgricole : EcranFiche, IEcranFiche { ... } // the interface public interface IEcranFiche { ... } // base class containing common implemented methods public class EcranFiche : UserControl { ... (ex: common interface implementation) } 
 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> 

有devise师创build的部分类定义,你可以通过InitializeComponent()方法定义简单的打开它。 然后,只需将UserControl的部分类inheritance性更改为BaseDataType(或您在类定义中指定的任何类)。

之后,你将会有警告:InitializeComponent()方法在子类中被隐藏。

因此,您可以将CustomControl作为base clas而不是UserControl,以避免在基类中进行部分定义(如同一个注释中所述)。