10

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.

3 Answers 3

22

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));
    ...
}
0
2

aside from the accepted answer which is correct for this situation, in some ocassaions it's maybe because you're not registering your property static, a DependencyProperty is not always dependent to DependencyObject, for instance you may are building a DependencyObject with a propery like DepObj.DepProp = "some prop" but the DependencyObject has not built yet, so DependencyPropertys must always be static (and which is public static should be readonly too due to programming logic ;))

wrong declaration of a DependencyProperty:

public DependencyProperty CProp = DependencyProperty.Register(
            "a",
            typeof(B),
            typeof(C),
            new PropertyMetadata(new C() { a= "default" }, CPropCallBack)
        );

correct declaration of a dependencyProperty:

public static readonly DependencyProperty CProp = DependencyProperty.Register(
            "a",
            typeof(B),
            typeof(C),
            new PropertyMetadata(new C() { a= "default" }, CPropCallBack)
        );
0
-1

same just happens to me, but with only one UserControl: "Vista.xaml" that is created multiple times into several tab pages of my main window.

I discover there is a lot of relations between the name of the property, the name of the variables and the other names across the app.

So i have to avoid name collisions across xaml Style, Code Behind, vars, funcs, etc. And that solve the problem for me.

this is the control in my "MainWindow.xaml"

<local:Vista Margin="8,10,10,0" Titulo="Here goes title"/>

this is the label inside my custom control "Vista" that shows the title set by the property "Titulo"

Codigo: 

  <Label x:Name="labelTitulo" Content="{Binding ElementName=ControlVista, 
  Path=Titulo}" Style="{StaticResource estiloTituloVista}" />

//////////

Here is the property wich is located in UserControl code-behind "Vista.xaml.cs"

enter code here
       public static readonly DependencyProperty TituloProperty =
            DependencyProperty.Register
            (
                "Titulo",
                typeof(string),
                typeof(Vista),
                new FrameworkPropertyMetadata("")
            );
        public string Titulo
        {
            get { return (string)GetValue(TituloProperty); }
            set { SetValue(TituloProperty, value); }
        }

enter code here

Which goes just before the main class of your UserControl

  public Vista()
     {
       InitializeComponent();

       if (Titulo != null && Titulo != "") { labelTitulo.Content = Titulo; }
           
     }

You have to name your UserControl in order to assign the property to one of it's childs.

in my case i named it "ControlVista"

This name is binding with the Content property of child label control "labelTitulo"

View of Vista.xaml design mode

This is how it looks in the mainwindow.xaml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.