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

