WPF教程五:附加依賴項屬性


  附加依賴項屬性是一個屬性本來不屬於對象自己,但是某些特定場景其他的對象要使用該對象在這種場景下的值。這個值只在這個場景下使用。基於這個需求設計出來的屬性。這里主要涉及到一個解耦問題。最大的優勢是在特定場景下使用的屬性,可以在特定場景下定義。這樣業務上不會導致代碼全部混在某個模塊里。提升代碼可維護性。

  我們舉例一段代碼。假設有個類Person。包含了身份ID(IdentityID),姓名(Name),出生年月(Birth date),性別(gender),民族(Nation)。 有一個School類,包含了年級(grade)。我們把用戶所在的年紀通過依賴項屬性的形式從Person類中解耦出去。這樣就可以更好的設計業務的關聯關系。

  我們創建繼承自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下的屬性對象。

 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代碼:

  <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下的位置。附加依賴項屬性,主要用於解耦。這篇文章就結束拉。

 <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、依賴項屬性和附加依賴項屬性,發現寫代碼的思路是不是一下就改變了很多?耦合正在慢慢的從設計層就變低了。

 

我創建了一個C#相關的交流群。用於分享學習資料和討論問題。歡迎有興趣的小伙伴:QQ群:542633085


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM