2009-05-20 43 views
0

我发现,当我使依赖属性,其中大多数冲突与用户控件中的属性名称,例如背景,宽度等,所以我的策略是前缀所有我的自定义属性与“The”所以我有,例如不使DependencyProperties与实际属性冲突的最佳方法是什么?

  • 后台运行此程序
  • TheWidth

我尝试使用它摆脱了警告的 “新” 的关键字,但是这将导致冲突,在运行时。

有没有人在定制用户控件中使用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 
} 

回答

5

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

这是什么新背景为背景? “该”?没有?那么它控制了什么背景?

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

属性名称现在应该是XXXXBackground。

+0

谢谢,有道理,也适用。 – 2009-05-25 10:03:32

0

你的意思是你想要覆盖?

在我来说,我不把宽度为DepedencyProperty

在我的自定义控制我有:

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

编译器显示我一个警告,但一切似乎工作。

相关问题