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称为 ...
随机推荐
- BZOJ3282: Tree
传送门 又是权限题= =,过了NOIp我就要去当一只权限狗! LCT裸题,get到了两个小姿势. 1.LCA操作应该在access中随时updata 2.Link操作可以更简单 void Link(i ...
- group by语句
- 深入JVM-性能监控工具
一.Linux下的性能监控工具 1.1 显式系统整体资源使用情况-top命令 top命令的输出可以分为两个部分:前半部分是系统统计信息,后半部分是进程信息. 在统计信息中,第一行是任务队列信息,他的结 ...
- mysql使用索引优化查询效率
索引的概念 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度.在没 ...
- 在不同的pyhon版本中切换
issue discription 在一台电脑上同时安装了python2.7和python3.5,怎样在这两个版本中切换调用? solution to the issue 进入python安装文件夹, ...
- HashMap与ArrayList互相嵌套的代码实现
HashMap嵌套ArrayList的代码实现 结果要求为 三国演义 吕布 周瑜笑傲江湖 令狐冲 林平之神雕侠侣 ...
- ef6 缓存的问题没有了
public static void Main(string[] args) { PMTestEntities db = new PMTestEntities(); var users = db.Us ...
- 基本药目录sop
http://db.yaozh.com/basicdir 基本药物 编辑 "基本药物"的概念, 由世界卫生组织于1977年提出,指的是能够满足基本医疗卫生需求,剂型适宜.保证供应. ...
- Java——private default protected public访问控制权限
访问控制权限 protected范例
- Android学习笔记——SQLite
该工程的功能是实现关于数据库的操作,即creat.update.insert.query.delete 调试的时候请用模拟器,用真机调试的时候进入cmd-adb shell,再进入cd data/da ...