WPF:依赖属性的应用
依赖属性与一般属性相比,提供了对资源引用、样式、动画、数据绑定、属性值继承、元数据重载以及WPF设计器的继承支持功能的支持。
下面的这个Demo来自《葵花宝典--WPF自学手册》。
1、MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="MyBrush" Color="Gold"/>
<Style x:Key="GreenButtonStyle">
<Setter Property="Control.Background" Value="Green"/>
</Style>
<local:BindingData x:Key="myDataSource"> </local:BindingData>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions> <Label HorizontalAlignment="Center" VerticalAlignment="Center">
资源支持
</Label>
<!--金色按钮的背景属性引用了画刷资源-->
<Button Grid.Row="0" Grid.Column="1" Name="resourceBtn" Background="{DynamicResource MyBrush}">
金色按钮
</Button>
<Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center">
样式支持
</Label>
<!--绿色按钮的Style属性引用了样式资源 -->
<Button Grid.Row="0" Grid.Column="3" Name="typeBtn" Style="{StaticResource GreenButtonStyle}">
绿色按钮
</Button>
<Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
动画支持
</Label>
<!--动画按钮的背景属性支持动画-->
<Button Grid.Row="1" Grid.Column="1" Name="animationBtn">
动画按钮
<Button.Background>
<SolidColorBrush x:Name="AnimBrush"/>
</Button.Background>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="AnimBrush"
Storyboard.TargetProperty="(SolidColorBrush.Color)"
From="Red" To="Green" Duration="0:0:5"
AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Label Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center">
数据绑定支持
</Label>
<!--按钮对绑定数据的支持-->
<Button Grid.Row="1" Grid.Column="3" Name="bindingBtn" Background="{Binding Source={StaticResource myDataSource},Path=ColorName}">
被绑定为红色!
</Button>
<!--依赖属性改变相应元素的字体大小-->
<Label Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
属性继承支持
</Label>
<Button Grid.Row="2" Grid.Column="1" Name="FontSizeWinBtn" Click="FontSizeWinBtn_Click">
设置窗口字体:16
</Button>
<Button Grid.Row="2" Grid.Column="2" Name="FontSizeBtn" Click="FontSizeBtn_Click">
设置按钮字体:8
</Button>
<Button Grid.Row="2" Grid.Column="3" Name="ResetFontSizeBtn" Click="ResetFontSizeBtn_Click">
重置窗口字体:12
</Button>
</Grid> </Window>
2、BindingData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WpfApplication1
{
class BindingData
{
public BindingData()
{
ColorName = "Red";
} private string name = "Red";
public string ColorName
{
get { return name; }
set
{
name = value; }
}
}
}
3、MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private double _oldFontSize = 0;
public MainWindow()
{
InitializeComponent(); _oldFontSize = FontSize;
}
//改变窗体所有元素的字体大小
private void FontSizeWinBtn_Click(object sender, RoutedEventArgs e)
{
FontSize = 16;
}
//仅改变该按钮的字体大小
private void FontSizeBtn_Click(object sender, RoutedEventArgs e)
{
this.FontSizeBtn.FontSize = 8;
}
//恢复字体大小
private void ResetFontSizeBtn_Click(object sender, RoutedEventArgs e)
{
FontSize = _oldFontSize;
this.FontSizeBtn.FontSize = _oldFontSize;
} }
}
4、运行结果

注意,
在第一次点击“设置窗口字体:16”的按钮时,窗口内所有字体都变成了16号大小的字体,因为这时,窗口内的元素都没有设置过FontSize这个属性,所以继承了窗口的FontSize属性值;
点击"设置按钮字体:8"按钮时,只有该按钮的字体变成8号,因为事件处理函数中只对它的FontSize属性赋值(this.FoniSizeBtn.FontSize=8;)。
之后,再次点击“设置窗口字体:16”的按钮时,"设置按钮字体:8"按钮的字体不会随之变成16号字体,因为该按钮的FontSize属性已经赋值过了,它就不再继承使用窗口的FontSize属性值了。
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方法,但这有一个前提,那就是需要在对象中拥有一个字段,才能在此字段的基础上获取或设置字段的值, ...
- WPF 依赖属性
依赖属性,简单的说,在WPF控件应用过程中,界面上直接可以引用的属性 如:<Button Content="aaa"></Button> Content称为 ...
随机推荐
- Android中定时执行任务的3种实现方法
在Android开发中,定时执行任务的3种实现方法: 一.采用Handler与线程的sleep(long)方法(不建议使用,java的实现方式)二.采用Handler的postDelayed(Runn ...
- JPA事务总结
http://www.soso.io/article/65405.html 事务管理是JPA中另一项重要的内容,了解了JPA中的事务管理,能够进一步掌握JPA的使用.事务管理是对一系列操作的管理,它最 ...
- mysql support chinese
1.创建table的时候使用utf8编码 create table tablename ( id int NOT NULL, content var_char(250) NOT NULL, CON ...
- python 安装包总结
PIL安装(Centos6.6) 1. 安装PIL所需的系统库 (centos6.6)yum install zlib zlib-devel -yyum install libjpeg libjpeg ...
- Bitmap四种属性
http://blog.csdn.net/rabbit_in_android/article/details/49967461 Bitmap: (1) public Bitmap (int w ...
- 深入JVM-锁与并发
一.锁在Java虚拟机中的实现与优化 1.1 偏向锁 偏向锁是JDK 1.6 提出的一种锁优化方式.其核心思想是,如果程序没有竞争,则取消之前已经取得锁的线程同步操作.也就说,若某一锁被线程获取后,便 ...
- document.execCommand 常用的方法
execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用如下格式: document.execCommand(sCommand[,交互方式, 动态参数]) , ...
- UML的目标
http://zhidao.baidu.com/link?url=ghQvzG70vSCSLyQcrHDTd7xt1aSWBR73lPIMxBCEPo1ktkq9cQ3EE9TXX1mZyHINkVA ...
- cocos2d 3.6 win7下的配置
我搭建cocos2.6的开发环境需要安装工具包括: 1.Visual Studio 2012(由于不兼容win7,需要安装Update 4)和虚拟光驱daemon tool,虚拟光驱的下载地址:htt ...
- 比较oracle数据的表结构
对比不同用户对象的异同,同时生成sql语句或直接提交到数据库,powerdesinger的比较实在是麻烦. pl/sql为我们提供了很好的工具 在pl/sql中的工具下“比较用户对象”,下即可实现: