2009-05-20 24 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; 
     } 
    } 

編譯器顯示我一個警告,但一切似乎工作。

相關問題