c# 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# wpf如何附加依赖项属性的详细内容,更多关于c# wpf附加依赖项属性的资料请关注编程宝库其它相关文章!

相信大家在阅读WPF相关GitHub开源项目源码时都会看见一串串这种数据:这种Geometry数据就是几何图形数据 为什么要用Geometry数据做图标?有一种做法是使用t ...