【发布时间】:2010-10-27 14:45:55
【问题描述】:
以下名为 DataTypeWholeNumber 的 WPF UserControl 有效。
现在我想制作一个名为 DataTypeDateTime 和 DataTypeEmail 等的 UserControl。
许多依赖属性将由所有这些控件共享,因此我想将它们的常用方法放入 BaseDataType 并让这些 UserControl 中的每一个都继承自此基本类型。
但是,当我这样做时,我得到 错误:部分声明可能没有不同的基类。
那么如何使用 UserControls 实现继承,以便共享功能都在基类中?
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 解决方法,请参阅:svetoslavsavov.blogspot.gr/2009/09/… 或对于在祖先中显式定义 GUI,请参阅 support.microsoft.com/kb/957231
标签: c# wpf xaml user-controls