ControlOne
public class ControlOne : TextEdit
{
public static readonly DependencyProperty AwesomeSauceProperty =
DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(FormTextEditInput));
public string AwesomeSauce
{
get { return GetValue(AwesomeSauceProperty) as string; }
set { SetValue(AwesomeSauceProperty, value); }
}
}
ControlTwo
public class ControlTwo : PasswordEdit
{
public static readonly DependencyProperty AwesomeSauceProperty =
DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(FormTextEditInput));
public string AwesomeSauce
{
get { return GetValue(AwesomeSauceProperty) as string; }
set { SetValue(AwesomeSauceProperty, value); }
}
}
而在XAML中,我只需这样做:
<controls:ControlOne AwesomeSauce="Yummy"/>
<controls:ControlTwo AwesomeSauce="Tummy"/>
我遇到了错误。
System.ArgumentException
'AwesomeSauce' property was already registered by 'ControlOne'.
您可能会问为什么我需要两个做同样事情的控件,我可以只创建数据模板并继续使用。但是我想要固执地说我需要不同类型的自定义控件做同样的事情。如果我可以使用通用类型的自定义控件将会很好,但我发现这是不可能的(对吗?)。
我也不想使用不同的名称,因为那只是一个解决问题的hack。
我只想让我的两个控件能够使用相同的名称作为它们的依赖属性。我错过了什么吗?或者说我完全不允许使用相同的名称?
我猜附加属性可能是解决方案,但我真的想推销一个自定义控件。