WPF属性(二)附加属性

附加属性是说一个属性本来不属于某个对象,但由于某种需求而被后来附加上,也就是把对象放入一个特定环境后对象才具有的属性就称为附加属性,附加属性的作用就是将属性与数据类型解耦,让数据类型的设计更加灵活,举例,一个TextBox被放在不同的布局容器中时就会有不同的布局属性,这些属性就是由布局容器为TextBox附加上的,附加属性的本质就是依赖属性,二者仅仅在注册和包装器上有一点区别

小技巧,在VS中输入propa后,连按两次tab键,可以添加好一个附加属性的框架,继续按tab键,可以继续修改附加属性的内容

 

举个例子,Person这个类,放在学校中就会获得年级这个属性,那么准备一个School类,School类继承DependencyObject,定义一个附加属性

 

[csharp] view plain copy
 
  1. public class School : DependencyObject  
  2. {  
  3.     public static int GetGrade(DependencyObject obj)  
  4.     {  
  5.         return (int)obj.GetValue(GradeProperty);  
  6.     }  
  7.   
  8.     public static void SetGrade(DependencyObject obj, int value)  
  9.     {  
  10.         obj.SetValue(GradeProperty, value);  
  11.     }  
  12.   
  13.     public static readonly DependencyProperty GradeProperty =  
  14.         DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School), new UIPropertyMetadata(0));  
  15. }  


可以看到,附加属性已依赖属性有两点不同:

 

一。附加属性使用的RegisterAttached方法,而依赖属性使用的是Register方法

二。附加属性使用两个方法进行包装,依赖属性使用CLR属性对GetValue和SetValue两个方法进行包装

如何使用School的GradeProperty呢,首先准备一个DependencyObject的派生类Human

 

[csharp] view plain copy
 
  1. public class Human : DependencyObject  
  2. {  
  3.   
  4. }  


使用这个附加属性

 

[csharp] view plain copy
 
  1. Human human = new Human();  
  2. School.SetGrade(human, 6);  
  3. MessageBox.Show(School.GetGrade(human).ToString());  

看看实例代码,如果要在一个3行3列的表格布局的最中间一格放置一个按钮,界面代码如下:

 

 

[html] view plain copy
 
  1. <Window x:Class="WpfApplication1.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="350" Width="525">  
  5.     <Grid ShowGridLines="True">  
  6.         <Grid.RowDefinitions>  
  7.             <RowDefinition />  
  8.             <RowDefinition />  
  9.             <RowDefinition />  
  10.         </Grid.RowDefinitions>  
  11.         <Grid.ColumnDefinitions>  
  12.             <ColumnDefinition />  
  13.             <ColumnDefinition />  
  14.             <ColumnDefinition />  
  15.         </Grid.ColumnDefinitions>  
  16.         <Button Grid.Row="1" Grid.Column="1" Content="OK" />  
  17.     </Grid>  
  18. </Window>  


运行后效果如图:

 

那么,如果用C#代码来实现这个效果,代码如下:

 

[csharp] view plain copy
 
  1. Grid grid = new Grid() { ShowGridLines = true };  
  2. grid.RowDefinitions.Add(new RowDefinition());  
  3. grid.RowDefinitions.Add(new RowDefinition());  
  4. grid.RowDefinitions.Add(new RowDefinition());  
  5.   
  6. grid.ColumnDefinitions.Add(new ColumnDefinition());  
  7. grid.ColumnDefinitions.Add(new ColumnDefinition());  
  8. grid.ColumnDefinitions.Add(new ColumnDefinition());  
  9.   
  10. Button button = new Button() { Content = "OK" };  
  11. Grid.SetRow(button, 1);  
  12. Grid.SetColumn(button, 1);  
  13.   
  14. grid.Children.Add(button);  
  15. Content = grid;  


效果与上图一致,附加属性的用法很奇怪吧,附加属性同时也是依赖属性,再附上一个例子,用两个滑块分别控制按钮在九宫格中的位置

 

 

[html] view plain copy
 
    1. <Window x:Class="WpfApplication1.MainWindow"  
    2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    4.         Title="MainWindow" Height="420" Width="525">  
    5.     <StackPanel>  
    6.         <Grid ShowGridLines="True" Height="300">  
    7.             <Grid.RowDefinitions>  
    8.                 <RowDefinition />  
    9.                 <RowDefinition />  
    10.                 <RowDefinition />  
    11.             </Grid.RowDefinitions>  
    12.             <Grid.ColumnDefinitions>  
    13.                 <ColumnDefinition />  
    14.                 <ColumnDefinition />  
    15.                 <ColumnDefinition />  
    16.             </Grid.ColumnDefinitions>  
    17.             <Button Content="OK" Grid.Row="{Binding ElementName=slider1, Path=Value}" Grid.Column="{Binding ElementName=slider2, Path=Value}" />  
    18.         </Grid>  
    19.         <Slider x:Name="slider1" Minimum="0" Maximum="2" />  
    20.         <Slider x:Name="slider2" Minimum="0" Maximum="2" />  
    21.     </StackPanel>  
    22. </Window>  

posted on 2017-07-26 09:22  alex5211314  阅读(237)  评论(0编辑  收藏  举报

导航