扫盲-wpf依赖属性
一、什么是依赖属性
依赖属性就是一种自己可以没有值,并且可以通过绑定从其他数据源获取值。依赖属性可支持WPF中的样式设置、数据绑定、继承、动画及默认值。
将所有的属性都设置为依赖属性并不总是正确的解决方案,具体取决于其应用场景。有时,使用私有字段实现属性的典型方法便能满足要求。MSDN中给出了下面几种应用依赖属性的场景:
1. 希望可在样式中设置属性。
2. 希望属性支持数据绑定。
3. 希望可使用动态资源引用设置属性。
4. 希望从元素树中的父元素自动继承属性值。
5. 希望属性可进行动画处理。
6. 希望属性系统在属性系统、环境或用户执行的操作或者读取并使用样式更改了属性以前的值时报告。
7. 希望使用已建立的、WPF 进程也使用的元数据约定,例如报告更改属性值时是否要求布局系统重新编写元素的可视化对象。
二、依赖属性的特点
1、属性变更通知
无论什么时候,只要依赖属性的值发生改变,wpf就会自动根据属性的元数据触发一系列的动作,这些动作可以重新呈现UI元素,也可以更新当前的布局,刷新数据绑定等等,这种变更的通知最有趣的特点之一就是属性触发器,它可以在属性值改变的时候,执行一系列自定义的动作,而不需要更改任何其他的代码来实现。通过下面的示例来演示属性变更通知
示例:当鼠标移动到Button按钮上面时,文字的前景色变为红色,离开时变为默认颜色黑色,采用传统方式和依赖属性两种方式实现:
(1)、使用传统方式实现,在Button按钮上定义MouseEnter和MouseLeave两个事件,分别处理鼠标移动到按钮上面和离开,XAML界面代码:

1 <Window x:Class="WpfDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Grid面板" Height="237" Width="525" WindowStartupLocation="CenterScreen">
5 <Grid >
6 <Button Height="30" Width="200" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" >鼠标移动到上面,前景色变为红色</Button>
7 </Grid>
8 </Window>

C#后台代码实现:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfDemo
17 {
18 /// <summary>
19 /// MainWindow.xaml 的交互逻辑
20 /// </summary>
21 public partial class MainWindow : Window
22 {
23 public MainWindow()
24 {
25 InitializeComponent();
26 }
27
28 /// <summary>
29 /// 鼠标移动到按钮上面
30 /// </summary>
31 /// <param name="sender"></param>
32 /// <param name="e"></param>
33 private void Button_MouseEnter(object sender, MouseEventArgs e)
34 {
35 Button btn = sender as Button;
36 if (btn != null)
37 {
38 btn.Foreground = Brushes.Red;
39 }
40 }
41
42 /// <summary>
43 /// 鼠标离开按钮
44 /// </summary>
45 /// <param name="sender"></param>
46 /// <param name="e"></param>
47 private void Button_MouseLeave(object sender, MouseEventArgs e)
48 {
49 Button btn = sender as Button;
50 if (btn != null)
51 {
52 btn.Foreground = Brushes.Black;
53 }
54 }
55 }
56 }

(2)使用依赖属性实现,XAML界面代码:

1 <Window x:Class="WpfDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Grid面板" Height="237" Width="525" WindowStartupLocation="CenterScreen">
5 <Grid >
6 <Button Height="30" Width="200">鼠标移动到上面,前景色变为红色
7 <Button.Style>
8 <Style TargetType="Button">
9 <Style.Triggers>
10 <Trigger Property="IsMouseOver" Value="true">
11 <Setter Property="Foreground" Value="Red"></Setter>
12 </Trigger>
13 </Style.Triggers>
14 </Style>
15 </Button.Style>
16 </Button>
17 </Grid>
18 </Window>

使用上面的两种方式都可以实现Button按钮的前景色改变,效果如下:
在判断属性IsMouseOver的值为false的时候,自动将Foreground的值改为之前的值,因此就不需要写IsMouseOver的值为false的时候,将Foreground的值改为Black。
2、属性值继承
是指属性值自顶向下沿着元素树进行传递。

1 <Window x:Class="WpfDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="依赖属性" Height="237" Width="525" FontSize="18" WindowStartupLocation="CenterScreen">
5 <Grid >
6 <StackPanel>
7 <TextBlock>我使用的是继承的fontsize</TextBlock>
8 <TextBlock FontSize="11">我使用的是自己的fontsize</TextBlock>
9 </StackPanel>
10 </Grid>
11 </Window>

界面运行效果:
3、节省内存空间
依赖属性和CLR属性在内存的使用上是截然不同的,每个CLR属性都包含一个非static的字段,因此当我们实例化一个类型的时候,就会创建该类型所拥有的所有CLR属性,也就是说一个对象所占用的内存在调用new操作进行实例化的时候就已经决定了、而wpf允许对象在创建的时候并不包含用于存储数据的空间,只保留在需要用到数据的时候能够获得该默认值,即用其他对象数据或者实时分配空间的能力。
三、如何自定义依赖属性
1、声明依赖属性变量。依赖属性的声明都是通过public static来公开一个静态变量,变量的类型必须是DependencyProperty
2、在属性系统中进行注册。使用DependencyProperty.Register方法来注册依赖属性,或者是使用DependencyProperty.RegisterReadOnly方法来注册
3、使用.NET属性包装依赖属性
在类上实现属性时,只要该类派生自 DependencyObject,便可以选择使用 DependencyProperty 标识符来标示属性,从而将其设置为依赖属性。其语法如下:

1 public static DependencyProperty TextProperty;
2 TextProperty =
3 DependencyProperty.Register("Text", //属性名称
4 typeof(string), //属性类型
5 typeof(TestDependencyPropertyWindow), //该属性所有者,即将该属性注册到那个类上
6 new PropertyMetadata("")); //属性默认值
7
8 public string Text
9 {
10 get { return (string)GetValue(TextProperty); }
11 set { SetValue(TextProperty, value); }
12 }

示例:自定义一个依赖属性,界面包括一个TextBox和TextBlock,TextBlock上面字体的前景色随TextBox里面输入的颜色而改变,如果TextBox里面输入的值可以转换成颜色,TextBlock字体的前景色会显示输入的颜色值,如果不能转换,显示默认的前景色。
1、在当前项目里面添加一个WPF版的用户控件,命名为“MyDependencyProperty”,在MyDependencyProperty.xaml.cs文件里面自定义一个依赖属性:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfDemo
17 {
18 /// <summary>
19 /// MyDependencyProperty.xaml 的交互逻辑
20 /// </summary>
21 public partial class MyDependencyProperty : UserControl
22 {
23 public MyDependencyProperty()
24 {
25 InitializeComponent();
26 }
27
28 //1、声明依赖属性变量
29 public static readonly DependencyProperty MyColorProperty;
30
31 //2、在属性系统中进行注册
32 static MyDependencyProperty()
33 {
34 MyColorProperty = DependencyProperty.Register("MyColor", typeof(string), typeof(MyDependencyProperty),
35 new PropertyMetadata("Red", (s, e) =>
36 {
37 var mdp = s as MyDependencyProperty;
38 if (mdp != null)
39 {
40 try
41 {
42 var color = (Color)ColorConverter.ConvertFromString(e.NewValue.ToString());
43 mdp.Foreground = new SolidColorBrush(color);
44 }
45 catch
46 {
47 mdp.Foreground = new SolidColorBrush(Colors.Black);
48 }
49 }
50
51 }));
52 }
53
54 //3、使用.NET属性包装依赖属性:属性名称与注册时候的名称必须一致,
55 //即属性名MyColor对应注册时的MyColor
56 public string MyColor
57 {
58 get
59 {
60 return (string)GetValue(MyColorProperty);
61 }
62 set
63 {
64 SetValue(MyColorProperty, value);
65 }
66 }
67 }
68 }

快速定义依赖属性的快捷方式:
输入propdp,连续按两下Tab健,自动生成定义依赖属性的语法。和输入cw连续按两下Tab健,自动生成Console.Write()一样。

1 public int MyProperty
2 {
3 get { return (int)GetValue(MyPropertyProperty); }
4 set { SetValue(MyPropertyProperty, value); }
5 }
6
7 // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
8 public static readonly DependencyProperty MyPropertyProperty =
9 DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

2、在MyDependencyProperty.xaml里面添加一个TextBlock

1 <UserControl x:Class="WpfDemo.MyDependencyProperty"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6 mc:Ignorable="d"
7 d:DesignHeight="300" d:DesignWidth="300">
8 <Grid>
9 <TextBlock>我是自定义的依赖属性</TextBlock>
10 </Grid>
11 </UserControl>

3、在MainWindow.xaml里面引用新创建的用户控件,并添加一个TextBox,用于输入颜色值,并将自定义的依赖属性MyColor绑定到TextBox

1 <Window x:Class="WpfDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:p="clr-namespace:WpfDemo"
5 Title="依赖属性" Height="237" Width="525" WindowStartupLocation="CenterScreen">
6 <Grid >
7 <StackPanel>
8 <TextBox Name="tbColor"></TextBox>
9 <p:MyDependencyProperty MyColor="{Binding Path=Text,ElementName=tbColor}" ></p:MyDependencyProperty>
10 </StackPanel>
11 </Grid>
12 </Window>

在设计界面显示的效果:
4、程序运行效果:
在TextBox里面输入正确的颜色值,前景色会显示为当前输入的颜色:
在TextBox里面输入错误的颜色值,前景色会显示为默认颜色:
扫盲-wpf依赖属性的更多相关文章
- WPF依赖属性详解
WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency P ...
- WPF自学入门(五)WPF依赖属性
在.NET中有事件也有属性,WPF中加入了路由事件,也加入了依赖属性.最近在写项目时还不知道WPF依赖属性是干什么用的,在使用依赖项属性的时候我都以为是在用.NET中的属性,但是确实上不是的,通过阅读 ...
- WPF依赖属性值源(BaseValueSource)
原文:WPF依赖属性值源(BaseValueSource) WPF依赖属性提供一个机制,可以获取依赖属性提供值的来源 其以BaseValueSource枚举表示 1.Default public ...
- WPF依赖属性(续)(3)依赖属性存储
原文:WPF依赖属性(续)(3)依赖属性存储 在之前的两篇,很多朋友参与了讨论,也说明各位对WPF/SL计数的热情,对DP系统各抒已见,当然也出现了一些分歧. 以下简称DP为依赖属性 ...
- WPF依赖属性(续)(1)
原文:WPF依赖属性(续)(1) 之前有写过几篇文章,详细地介绍了依赖属性的基本使用方法,如果你不想了解其内部实现机制的话,那么通过那两篇文章的介绍,足以应付平时的应用 ...
- WPF依赖属性(续)(2)依赖属性与附加属性的区别
原文:WPF依赖属性(续)(2)依赖属性与附加属性的区别 接上篇,感谢各位的评论,都是认为依赖属性的设计并不是为了节省内存,从大的方面而讲是如此.样式,数据绑定,动画样样都离不开它.这篇 ...
- 监听WPF依赖属性
原文:监听WPF依赖属性 当我们使用依赖属性的时候,有时需要监听它的变化,这在写自定义控件的时候十分有用, 下面介绍一种简单的方法. 如下使用DependencyPropertyDescripto ...
- WPF依赖属性的正确学习方法
前言 我在学习WPF的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...
- WPF 依赖属性前言
WPF 依赖属性前言 在.net中,我们可以属性来获取或设置字段的值,不需要在编写额外的get和set方法,但这有一个前提,那就是需要在对象中拥有一个字段,才能在此字段的基础上获取或设置字段的值, ...
随机推荐
- Android 获取虚拟按键的高度
//获取虚拟按键的高度 public static int getNavigationBarHeight(Context context) { int result = 0; if (hasNavBa ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...
- 使用eclipse搭建maven多module项目(构建父子项目)
创建空maven项目 File–>new–>project… 2.next 3.next 4.finish 5.配置pom.xml <project xmlns="http ...
- hibernate 数据缓存
http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html,
- UNP学习笔记(第十六章 非阻塞I/O)
套接字的默认状态时阻塞的 可能阻塞的套接字调用可分为以下4类: 1.输入操作,包括read.readv.recv.recvfrom和recvmsg. 2.输入操作,包括write.writev.sen ...
- apue学习笔记(第五章 标准I/O)
本章讲述标准I/O库 流和FILE对象 对于标准I/O库,它们的操作是围绕流进行的.流的定向决定了所读.写的字符是单字节还是多字节的. #include <stdio.h> #includ ...
- metaq入门部署到实战
初识metaq zookeeper部署,这里单机zk为例. wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.5/zookeep ...
- ExtJs4学习(一):正确认识ExtJs4
认识ExtJs 1.Javat能用ExtJs吗? 它是展现层的技术,与JS,HTML,CSS有关.至于server端是.Net,还是PHP等无关. 2.ExtJs适合什么样的项目? 依照官方的说法,E ...
- iOS之手势滑动返回功能
iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...
- ASP.NET MVC 扩展自定义视图引擎支持多模板&动态换肤skins机制
ASP.NET mvc的razor视图引擎是一个非常好的.NET MVC 框架内置的视图引擎.一般情况我们使用.NET MVC框架为我们提供的这个Razor视图引擎就足够了.但是有时我们想在我们的 ...