views:

33

answers:

2

I find that when I make Dependency Properties, most of them conflict with names of properties in the UserControl, e.g Background, Width, etc. so my strategy is to prefix all of my custom properties with "The" so I have, e.g.

  • TheBackground
  • TheWidth

etc.

I tried using the "new" keyword which gets rid of the warning, but that leads to conflicts at runtime.

Has anyone come upon better naming strategies for DependencyProperties in custom user controls?

public partial class SmartForm : UserControl
{
    public SmartForm()
    {
        InitializeComponent();
        DataContext = this;

        TheBackground = "#FFD700";
    }

    #region DependencyProperty: TheBackground
    public string TheBackground
    {
        get
        {
            return (string)GetValue(TheBackgroundProperty);
        }
        set
        {
            SetValue(TheBackgroundProperty, value);
        }
    }

    public static readonly DependencyProperty TheBackgroundProperty =
        DependencyProperty.Register("TheBackground", typeof(string), typeof(SmartForm),
        new FrameworkPropertyMetadata());
    #endregion
}
+4  A: 

If your UserControl has a background property, why do you have to add another one?

What is this new background a background for? "The"? No? Then what background does it control?

Finish this sentence: "This is the background color for the XXXX control."

The property name should now be XXXXBackground.

Will
Thanks, makes sense, works as well.
Edward Tanguay
A: 

You mean you want a override??

In my case I don't put Width as a DepedencyProperty

in my custom control I have:

    public double Width
    {
        get
        {
            if (_backgroundImage != null)
                return _backgroundImage.Width;
            else
                return double.NaN;
        }
        set
        {
            if (_backgroundImage != null)
                _backgroundImage.Width = value;
        }
    }

The compiler shows me a warning but everything seems to work.

Junior Mayhé