附加依賴項屬性是一個屬性原本不屬於對象本身,可是某些特定場景其餘的對象要使用該對象在這種場景下的值。這個值只在這個場景下使用。基於這個需求設計出來的屬性。這裏主要涉及到一個解耦問題。最大的優點是在特定場景下使用的屬性,能夠在特定場景下定義。這樣業務上不會致使代碼所有混在某個模塊裏。提高代碼可維護性。canvas
咱們舉例一段代碼。假設有個類Person。包含了身份ID(IdentityID),姓名(Name),出生年月(Birth date),性別(gender),民族(Nation)。 有一個School類,包含了年級(grade)。咱們把用戶所在的年紀經過依賴項屬性的形式從Person類中解耦出去。這樣就能夠更好的設計業務的關聯關係。ide
咱們建立繼承自DependencyObject的School類。使用propa 快捷方式建立屬性。咱們給School添加了一個附加依賴項屬性GradeProperty。佈局
代碼以下:學習
public class School : DependencyObject { public static int GetGrade(DependencyObject obj) { return (int)obj.GetValue(GradeProperty); } public static void SetGrade(DependencyObject obj, int value) { obj.SetValue(GradeProperty, value); } // Using a DependencyProperty as the backing store for Grade. This enables animation, styling, binding, etc... public static readonly DependencyProperty GradeProperty = DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School), new PropertyMetadata(0)); }
咱們繼續建立Person類,使用propdp建立。由於咱們已經學習了依賴項屬性,因此咱們只使用依賴項屬性來建立全部Person下的屬性對象。spa
public class Person : DependencyObject { public string IdentityID { get { return (string)GetValue(IdentityIDProperty); } set { SetValue(IdentityIDProperty, value); } } // Using a DependencyProperty as the backing store for IdentityID. This enables animation, styling, binding, etc... public static readonly DependencyProperty IdentityIDProperty = DependencyProperty.Register("IdentityID", typeof(string), typeof(Person)); public string Name { get { return (string)GetValue(NamePropertyProperty); } set { SetValue(NamePropertyProperty, value); } } // Using a DependencyProperty as the backing store for NameProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty NamePropertyProperty = DependencyProperty.Register("Name", typeof(string), typeof(Person)); public DateTime BirthDate { get { return (DateTime)GetValue(BirthDateProperty); } set { SetValue(BirthDateProperty, value); } } // Using a DependencyProperty as the backing store for BirthDate. This enables animation, styling, binding, etc... public static readonly DependencyProperty BirthDateProperty = DependencyProperty.Register("BirthDate", typeof(DateTime), typeof(Person)); public bool Gender { get { return (bool)GetValue(GenderProperty); } set { SetValue(GenderProperty, value); } } // Using a DependencyProperty as the backing store for Gender. This enables animation, styling, binding, etc... public static readonly DependencyProperty GenderProperty = DependencyProperty.Register("Gender", typeof(bool), typeof(Person)); public string Nation { get { return (string)GetValue(NationProperty); } set { SetValue(NationProperty, value); } } // Using a DependencyProperty as the backing store for Nation. This enables animation, styling, binding, etc... public static readonly DependencyProperty NationProperty = DependencyProperty.Register("Nation", typeof(string), typeof(Person)); }
咱們建立一個按鈕來給Person設置一個附加依賴項屬性。學校的年級。設計
xaml代碼和對應的cs代碼:code
<Button Content="點擊給Pserson對象在對應的業務上附加依賴性屬性" Click="AddPsersonAttachedProperty_Click"/>
private void AddPsersonAttachedProperty_Click(object sender, RoutedEventArgs e) { Person person = new Person(); School.SetGrade(person, 1); var grade = School.GetGrade(person); MessageBox.Show(grade.ToString()) ; }
我門經過點擊按鈕建立了一個Person對象。使用School.SetGrade(person,1)來調用了我門添加在School類下面的附加依賴項屬性。我門給Person設置了附加依賴項屬性,是School下的Grade依賴項屬性值爲1。關鍵點:附加依賴項屬性的適用場景是給當前對象添加在某些場景下使用的屬性值。 這個時候咱們可使用School.GetGrade(person)讀取這個對象中的依賴項屬性。依賴項屬性理解到這裏就行。Grid和子元素在Grid中放置行列的原理和這個對依賴項屬性使用是同樣的。只是有一些後續的邏輯。目前只要會使用能看懂就好。後續會在自定義控件中,設計本身的佈局呈現控件時,在設計階段講解哪些應該使用依賴項屬性,哪些使用附加依賴項屬性。對象
用一個經過附加屬性移動Slider控制小球在Canvas下的例子。 給一個不存在Left 和top屬性的小球。添加一個在父容器canvas的附加依賴屬性,用來控制小球在Canvas下的位置。附加依賴項屬性,主要用於解耦。這篇文章就結束拉。blog
<Canvas> <Slider x:Name="sliderX" Canvas.Top="10" Canvas.Left="10" Width="200" Minimum="50" Maximum="200"/> <Slider x:Name="sliderY" Canvas.Top="40" Canvas.Left="10" Width="200" Minimum="50" Maximum="200"/> <Ellipse Fill="Blue" Width="30" Height="30" Canvas.Left="{Binding ElementName=sliderX,Path=Value}" Canvas.Top="{Binding ElementName=sliderY,Path=Value}"/> </Canvas>
咱們使用Canvas做爲畫板,在裏面放入2個Slider一個控制水平位置,一個控制垂直位置。咱們經過綁定Slider的Value到Ellipse在Canvas下的附加依賴項屬性Canvas.Left和Canvas.Top來繼承
控制小球的垂直和水平位置。來演示如何解耦Ellipse和canvas的佈局關係。經過前面瞭解的的binding、依賴項屬性和附加依賴項屬性,發現寫代碼的思路是否是一下就改變了不少?耦合正在慢慢的從設計層就變低了。