I created two custom controls that are exactly the same except that they are of two different types.
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); }
}
}
And in XAML i simply do this
<controls:ControlOne AwesomeSauce="Yummy"/>
<controls:ControlTwo AwesomeSauce="Tummy"/>
and I get error
System.ArgumentException
'AwesomeSauce' property was already registered by 'ControlOne'.
You may be asking why I need two controls that do the same things, I can just create Data Templates and move on. But I want to be stubborn and say that I need custom controls of different types that do the same thing. It be nice if I could use custom controls of a generic type, but I found out that that is not possible (right?).
I also don't want to use different names because that would just be a hack to the problem.
I just want my two controls to be able to use the same names for their dependency properties. Is there something I'm missing here? or I'm I just flat out not allowed to use the same names?
I'm guessing that attached properties would be the solution for this, but I really want to push for a custom control.
The third parameter ownerType
of the DependencyProperty.Register
method must be the type of the class that registers the property, i.e. ControlOne and ControlTwo in your case:
public class ControlOne : TextEdit
{
public static readonly DependencyProperty AwesomeSauceProperty =
DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(ControlOne));
...
}
public class ControlTwo : TextEdit
{
public static readonly DependencyProperty AwesomeSauceProperty =
DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(ControlTwo));
...
}