c# wpf如何附加依賴項(xiàng)屬性
附加依賴項(xiàng)屬性是一個屬性本來不屬于對象自己,但是某些特定場景其他的對象要使用該對象在這種場景下的值。這個值只在這個場景下使用?;谶@個需求設(shè)計(jì)出來的屬性。這里主要涉及到一個解耦問題。最大的優(yōu)勢是在特定場景下使用的屬性,可以在特定場景下定義。這樣業(yè)務(wù)上不會導(dǎo)致代碼全部混在某個模塊里。提升代碼可維護(hù)性。
我們舉例一段代碼。假設(shè)有個類Person。包含了身份ID(IdentityID),姓名(Name),出生年月(Birth date),性別(gender),民族(Nation)。 有一個School類,包含了年級(grade)。我們把用戶所在的年紀(jì)通過依賴項(xiàng)屬性的形式從Person類中解耦出去。這樣就可以更好的設(shè)計(jì)業(yè)務(wù)的關(guān)聯(lián)關(guān)系。
我們創(chuàng)建繼承自DependencyObject的School類。使用propa 快捷方式創(chuàng)建屬性。我們給School添加了一個附加依賴項(xiàng)屬性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)); }
我們繼續(xù)創(chuàng)建Person類,使用propdp創(chuàng)建。因?yàn)槲覀円呀?jīng)學(xué)習(xí)了依賴項(xiàng)屬性,所以我們只使用依賴項(xiàng)屬性來創(chuàng)建所有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)); }
我們創(chuàng)建一個按鈕來給Person設(shè)置一個附加依賴項(xiàng)屬性。學(xué)校的年級。
xaml代碼和對應(yīng)的cs代碼:
<Button Content="點(diǎn)擊給Pserson對象在對應(yīng)的業(yè)務(wù)上附加依賴性屬性" 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()) ; }
我門通過點(diǎn)擊按鈕創(chuàng)建了一個Person對象。使用School.SetGrade(person,1)來調(diào)用了我門添加在School類下面的附加依賴項(xiàng)屬性。我門給Person設(shè)置了附加依賴項(xiàng)屬性,是School下的Grade依賴項(xiàng)屬性值為1。關(guān)鍵點(diǎn):附加依賴項(xiàng)屬性的適用場景是給當(dāng)前對象添加在某些場景下使用的屬性值。 這個時(shí)候我們可以使用School.GetGrade(person)讀取這個對象中的依賴項(xiàng)屬性。依賴項(xiàng)屬性理解到這里就行。Grid和子元素在Grid中放置行列的原理和這個對依賴項(xiàng)屬性使用是一樣的。只是有一些后續(xù)的邏輯。目前只要會使用能看懂就好。后續(xù)會在自定義控件中,設(shè)計(jì)自己的布局呈現(xiàn)控件時(shí),在設(shè)計(jì)階段講解哪些應(yīng)該使用依賴項(xiàng)屬性,哪些使用附加依賴項(xiàng)屬性。
用一個通過附加屬性移動Slider控制小球在Canvas下的例子。 給一個不存在Left 和top屬性的小球。添加一個在父容器canvas的附加依賴屬性,用來控制小球在Canvas下的位置。附加依賴項(xiàng)屬性,主要用于解耦。這篇文章就結(jié)束拉。
<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下的附加依賴項(xiàng)屬性Canvas.Left和Canvas.Top來
控制小球的垂直和水平位置。來演示如何解耦Ellipse和canvas的布局關(guān)系。通過前面了解的的binding、依賴項(xiàng)屬性和附加依賴項(xiàng)屬性,發(fā)現(xiàn)寫代碼的思路是不是一下就改變了很多?耦合正在慢慢的從設(shè)計(jì)層就變低了。
以上就是c# wpf如何附加依賴項(xiàng)屬性的詳細(xì)內(nèi)容,更多關(guān)于c# wpf附加依賴項(xiàng)屬性的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
c#中的interface abstract與virtual介紹
abstract 與virtual : 方法重寫時(shí)都使用 override 關(guān)鍵字,interface中的方法和abstract方法都要求實(shí)現(xiàn)2013-07-07使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的方法
這篇文章主要給大家介紹了關(guān)于如何使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2019-03-03