一个WPF用户控件如何继承一个WPF用户控件? [英] How can a WPF UserControl inherit a WPF UserControl?
问题描述
下面的WPF用户控件名为 DataTypeWholeNumber 这工作。
现在我想打一个叫做用户控件的 DataTypeDateTime 和 DataTypeEmail 等。
许多依赖属性将被所有这些控制共享的,所以我想提出的他们共同的方法到BaseDataType 并有每个用户控件从这个基类继承。
然而,当我这样做,我得到的的错误:部分声明可能不会有不同的基类的
所以,我怎么能实现与用户控件,以便共享的功能继承所有基类?
使用System.Windows;
使用System.Windows.Controls的;命名空间TestDependencyProperty827.DataTypes
{
公共部分类DataTypeWholeNumber:BaseDataType
{
公共DataTypeWholeNumber()
{
的InitializeComponent();
的DataContext =这一点; //默认
TheWidth = 200;
} 公共字符串TheLabel
{
得到
{
返回(串)的GetValue(TheLabelProperty);
}
组
{
的SetValue(TheLabelProperty,值);
}
} 公共静态只读的DependencyProperty TheLabelProperty =
DependencyProperty.Register(TheLabel的typeof(串)的typeof(BaseDataType)
新FrameworkPropertyMetadata());
公共字符串TheContent
{
得到
{
返回(串)的GetValue(TheContentProperty);
}
组
{
的SetValue(TheContentProperty,值);
}
} 公共静态只读的DependencyProperty TheContentProperty =
DependencyProperty.Register(TheContent的typeof(串)的typeof(BaseDataType)
新FrameworkPropertyMetadata());
公众诠释TheWidth
{
得到
{
返回(INT)的GetValue(TheWidthProperty);
}
组
{
的SetValue(TheWidthProperty,值);
}
} 公共静态只读的DependencyProperty TheWidthProperty =
DependencyProperty.Register(TheWidth的typeof(INT)的typeof(DataTypeWholeNumber)
新FrameworkPropertyMetadata()); }
}
确保您已在XAML改变了首个标记也将从新的基本类型继承
所以
<用户控件X:类=TestDependencyProperty827.DataTypes.DataTypeWholeNumber
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:S =CLR的命名空间:系统;装配= mscorlib程序
>
变为
< myTypes:BaseDataType X:类=TestDependencyProperty827.DataTypes.DataTypeWholeNumber
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:S =CLR的命名空间:系统;装配= mscorlib程序
的xmlns:myTypes =CLR的命名空间:TestDependencyProperty827.DataTypes
>
所以,总结一下完整的答案,包括从下面的评论中额外的细节:
- 的基类不应该包括XAML文件。它定义在一个单一的(非局部)CS文件并定义它直接从用户控件继承。
- 确保从子类无论在CS code隐藏文件和XAML的第一个标签的基类继承(如上图所示)。
The following WPF UserControl called DataTypeWholeNumber which works.
Now I want to make a UserControl called DataTypeDateTime and DataTypeEmail, etc.
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.
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());
}
}
Ensure that you have changed the first tag in the xaml to also inherit from your new basetype
So
<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"
>
becomes
<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:
- The base class should not include a xaml file. Define it in a single (non-partial) cs file and define it to inherit directly from Usercontrol.
- Ensure that the subclass inherits from the base class both in the cs code-behind file and in the first tag of the xaml (as shown above).
这篇关于一个WPF用户控件如何继承一个WPF用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!