以下WPF用戶控件名爲DataTypeWholeNumber它的工作原理。WPF UserControl如何繼承WPF UserControl?
現在我想打一個用戶控件稱爲DataTypeDateTime和DataTypeEmail等
許多依賴屬性將所有這些控件可以共享,所以我希望把自己的常見的方法進入BaseDataType並且每個這些UserControls都從此基類型繼承。
然而,當我這樣做,我得到錯誤:部分聲明可能不會有不同的基類。
那麼我如何實現UserControl的繼承,所以共享的功能都在基類中?
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());
}
}
有關WPF解決方法的可視化繼承,請參閱:http://svetoslavsavov.blogspot.gr/2009/09/user-control-inheritance-in-wpf.html或用於顯式定義祖先中的GUI,請參閱http:// support.microsoft.com/kb/957231 – 2015-01-10 01:44:17