WPF的依赖属性

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

开发工具与关键技术:VS,C#,WPF

作者:邓晓迪

撰写时间:2019年0528

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. 概述:依赖属性和CRL属性类似,提供一个实例级私有字段的访问封装,通过GetValue和SetValue访问器实现属性的读写操作,最重要一个特点是属性值依赖于一个或者多个数据源,提供这些数据源的方式也可以不同,所以依赖属性就是一种自己可以没有值,并且可以通过绑定从其他数据源获取值。

下面我分别用传统方式和依赖属性方式来实现当鼠标移动到圆上时,给其加上一个大小为40的红色轮廓,离开后变回绿色这一效果,看看它们的区别,效果图如下:

WPF的依赖属性

  • 使用传统方式实现:XAML代码:
  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.         WindowStartupLocation="CenterScreen"  
  5.         Title="MainWindow" Height="350" Width="525">  
  6.     <Grid>  
  7.         <TextBlock TextAlignment="Center" FontSize="16" Margin="0 25 0 0">  
  8.             鼠标移动到圆上,给它一个红色的轮廓  
  9.         </TextBlock>  
  10.         <Ellipse Height="200" Width="200" Fill="Green" MouseEnter="Ellipse_MouseEnter" MouseLeave="Ellipse_MouseLeave"></Ellipse>  
  11.     </Grid>  
  12. </Window>

C#后台代码实现:

  1. using System.Windows;  
  2. using System.Windows.Input;  
  3. using System.Windows.Media;  
  4. using System.Windows.Shapes;  
  5.   
  6. namespace WpfApplication1  
  7. {  
  8.     /// <summary>  
  9.     /// MainWindow.xaml 的交互逻辑  
  10.     /// </summary>  
  11.     public partial class MainWindow : Window  
  12.     {  
  13.         public MainWindow()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private void Ellipse_MouseEnter(object sender, MouseEventArgs e)  
  19.         {  
  20.             //创建Ellipse对象    
  21.             /*sender 是 private void Ellipse_MouseEnter(object sender,MouseEventArgs e)   
  22.               传入的参数,as 是类型转换  
  23.               sender as Ellipse 意思就是将 sender这个object对象转换为Ellipse对象  
  24.             */  
  25.             Ellipse elp = sender as Ellipse;  
  26.             if (elp != null)  
  27.                 {  
  28.                 elp.Stroke = Brushes.Red;  
  29.                 elp.StrokeThickness = 40;  
  30.             }  
  31.         }  
  32.   
  33.         private void Ellipse_MouseLeave(object sender, MouseEventArgs e)  
  34.         {  
  35.             Ellipse elp = sender as Ellipse;  
  36.             if (elp != null)  
  37.             {  
  38.                 elp.StrokeThickness = 0;  
  39.             }  
  40.         }  
  41.     }  
  42. }
  • 使用依赖属性实现:XAML代码:
  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.         WindowStartupLocation="CenterScreen"  
  5.         Title="MainWindow" Height="350" Width="525">  
  6.     <Grid>  
  7.         <TextBlock TextAlignment="Center" FontSize="16" Margin="0 25 0 0">  
  8.             鼠标移动到圆上,给它一个红色的轮廓  
  9.         </TextBlock>  
  10.         <Ellipse Height="200" Width="200" Fill="Green">  
  11.             <Ellipse.Style>  
  12.                 <Style TargetType="Ellipse">  
  13.                     <!--样式触发器-->  
  14.                     <Style.Triggers>  
  15.                         <!--IsMouseOver(鼠标悬停)鼠标位于控件上-->  
  16.                         <Trigger Property="IsMouseOver" Value="true">  
  17.                             <!--大小为40的红色轮廓-->  
  18.                             <Setter Property="Stroke"  Value="Red"/>  
  19.                             <Setter Property="StrokeThickness"  Value="40"/> 
  20.                         </Trigger>  
  21.                     </Style.Triggers>  
  22.                 </Style>  
  23.             </Ellipse.Style>  
  24.         </Ellipse>  
  25.     </Grid>  
  26. </Window>

由上面两种方式实现同一效果的代码来看,传统方式比依赖属性多了一个后台代码才能实现,而依赖属性无论什么时候,只要依赖属性的值发生改变,wpf就会自动根据属性的元数据触发一系列的动作,这些动作可以重新呈现UI元素,也可以更新当前的布局,刷新数据绑定等等,这种变更的通知最有趣的特点之一就是属性触发器,它可以在属性值改变的时候,执行一系列自定义的动作,而不需要更改任何其他的代码来实现。