2010-08-09 28 views
3

在本書Visual C# 2010 Recipes: A Problem-Solution Approach中,有一個演示依賴項屬性功能的示例。該示例描述了一個UserControl,它具有兩個屬性TextFontWeight和TextContent,它們分別依附於它們的依賴項屬性TextFontWeightProperty和TextContentProperty。有關依賴項屬性示例的問題

的XAML代碼後面去如下

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace TestWpfApplication 
{ 
    /// <summary> 
    /// Interaction logic for TestDpControl.xaml 
    /// </summary> 
    public partial class TestDpControl : UserControl 
    { 
     public TestDpControl() 
     { 
      InitializeComponent(); 
      DataContext = this; 
     } 

     public FontWeight TextFontWeight 
     { 
      get { return (FontWeight)GetValue(TextFontWeightProperty); } 
      set { SetValue(TextFontWeightProperty, value); } 

     } 

     public string TextContent 
     { 
      get { return (string)GetValue(TextContentProperty); } 
      set { SetValue(TextContentProperty, value); } 


     } 

     public static readonly DependencyProperty TextContentProperty = 
      DependencyProperty.Register(
      "TextContent", 
      typeof(string), 
      typeof(TestDpControl), 

      new FrameworkPropertyMetadata(
       "Default Value", 
       FrameworkPropertyMetadataOptions.AffectsArrange 
       & FrameworkPropertyMetadataOptions.AffectsMeasure 
       & FrameworkPropertyMetadataOptions.AffectsRender)); 


     public static readonly DependencyProperty TextFontWeightProperty = 
      DependencyProperty.Register(
      "TextFontWeight", 
      typeof(FontWeight), 
      typeof(TestDpControl), 
      new FrameworkPropertyMetadata(FontWeights.Normal, 
       FrameworkPropertyMetadataOptions.AffectsArrange 
       & FrameworkPropertyMetadataOptions.AffectsMeasure 
       & FrameworkPropertyMetadataOptions.AffectsRender, 
       TextFontWeight_PropertyChanged, 
       TextFontWeight_CoerceValue)); 

     private static object TextFontWeight_CoerceValue(DependencyObject d, object value) 
     { 
      FontWeight fontWeight = (FontWeight)value; 
      if (fontWeight == FontWeights.Bold || fontWeight == FontWeights.Normal) 
      { 
       return fontWeight; 
      } 
      return FontWeights.Normal; 

     } 

     private static void TextFontWeight_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      TestDpControl myControl = d as TestDpControl; 

      if (myControl != null) 
      { 
       FontWeight fontWeight = (FontWeight)e.NewValue; 
       string fontWeightName; 

       if (fontWeight == FontWeights.Bold) 
        fontWeightName = "Bold"; 
       else 
        fontWeightName = "Normal"; 
       myControl.txblFontWeight.Text = string.Format("Font weight set to:{0}.", fontWeightName); 

      } 


     } 
    }} 

我無法理解有什麼好處呢依賴屬性提供了此方案。在這種情況下,返回FontWeights.BoldTextFontWeight"Default Value"TextContent的簡單獲得者是否就夠了?如果有人能夠說明這個特定例子中的依賴屬性的權力,或者在這種情況下它的使用是否謹慎,那將是非常好的。

由於

回答

2

依賴屬性的全部要點,正如其名稱(種斜)認爲,是他們的價值可以依靠別的東西。綁定,值繼承和動畫是這樣做的三種主要方式:

  • 綁定:屬性的值取決於綁定到的數據源。
  • 值繼承:該屬性的值取決於其父/祖先對象中的值。
  • 動畫:屬性的值取決於故事板,它隨着時間的推移正在改變它的值。

您發佈的內容不會「顯示依賴項屬性的威力」。它展示了你必須經歷的事情,才能充分利用依賴屬性的力量。您不能僅使用後臺字段創建CLR屬性;您必須將獲取器和設置器分配給GetValueSetValue,以便CLR屬性和依賴項屬性系統執行相同的操作。

+0

感謝您的所有澄清。這很有幫助。 – 2010-08-10 18:32:49

2

一個優點使用DependencyProperties是,它可以很容易地結合在XAML這些屬性(數據綁定是的WPF IMHO的最強大的一個方面)。

下面是一些依賴屬性的額外功率的出色新手必看:

http://joshsmithonwpf.wordpress.com/2007/05/16/demystifying-dependency-properties/

請注意,您仍然可以綁定到XAML正常性能,但在某些情況下,你必須實現INotifyPropertyChanged保持UI同步。

1

因爲在你的書的例子是非常基本的,你是對的,一個簡單的setter /吸氣將可能足以適應這種特殊情況。但是,如果你想使用在一般情況下控制你應該考慮DependencyProperties的 好處:

  • 它們可綁定與WPF數據綁定(重要例如,用於MVVM模式)
  • 你看到他們在XAML編輯器
  • 可以繼承DependencyProperties通過控制所謂的附加屬性不相知
  • ...

的列表更LON蒙古包。如果您打算在WPF中使用自定義控件/用戶控件,您將看到相當快,DependencyProperties對於GUI開發非常有用且必不可少,而不僅僅是Mircosoft的推薦。

希望這是有幫助的小