0

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

2 回答 2

5

如果您的 UserControl 具有背景属性,为什么还要添加另一个?

这个新背景是什么背景?“这”?不?那么它控制什么背景呢?

完成这句话:“这是 XXXX 控件的背景颜色。”

属性名称现在应该是 XXXXBackground。

于 2009-05-20T14:27:50.223 回答
0

你的意思是你想要一个覆盖?

就我而言,我没有将 Width 作为 DepedencyProperty

在我的自定义控件中,我有:

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

编译器向我显示警告,但一切似乎都正常。

于 2009-05-22T22:06:27.527 回答