没有DependencyProperties与实际属性冲突的最佳方法是什么?

Edw*_*uay 0 wpf user-controls dependency-properties

我发现当我创建依赖属性时,大多数都与UserControl中的属性名称冲突,例如背景,宽度等.所以我的策略是在我的所有自定义属性前加上"The",所以我有,例如

  • 的背景
  • 宽度

等等

我尝试使用"new"关键字来消除警告,但这会导致运行时发生冲突.

有没有人在自定义用户控件中为DependencyProperties提供更好的命名策略?

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
}
Run Code Online (Sandbox Code Playgroud)

小智 5

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

这个新背景是什么背景?"该"?没有?那么它控制的背景是什么?

完成这句话:"这是XXXX控件的背景颜色."

现在属性名称应为XXXXBackground.