类型是DependencyProperty的属性是依赖属性

依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值。

为什么引入依赖属性:

MSDN原文 One of the primary architectural philosophies used in building WPF was a preference for properties over methods or events.

WPF的设计思想是侧重属性胜于方法和事件。

实例  

依赖属性对资源引用的支持

设置Button的Background为金色

APP.xaml

<Application.Resources>
  <SolidColorBrush x:Key="MyBrush" Color="Gold" />
</Application.Resources>

MainWindow.xaml

<Button Grid.Row="" Grid.Column="" Name="btn1" Margin="" Background="{DynamicResource MyBrush}">Golden Button</Button>

依赖属性对样式的支持

App.xaml

<Application.Resources>
  <Style x:Key="GreenButtonStyle">
    <Setter Property="Control.Background" Value="Green" />
  </Style>
</Application.Resources>

MainWindow.xaml

 <Button Grid.Row="" Grid.Column="" Name="btn2" Margin="" Style="{StaticResource GreenButtonStyle}">Green Button</Button>

依赖属性对动画的支持

<Button Grid.Row="" Grid.Column="" Name="btn3" Margin="">
<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>
Animation Button</Button>

依赖属性对数据绑定的支持

新建数据类型

    class BindingData
{
public BindingData() { } private string colorName = "Red"; public string ColorName
{
get { return this.colorName; }
set { this.colorName = value; }
}
}

App.xaml

<Application x:Class="Alex_WPFAPPDemo01.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Alex_WPFAPPDemo01"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:BindingData x:Key="DataSource" />
</Application.Resources>
</Application>

MainWindow.xaml

 <Button Grid.Row="" Grid.Column="" Name="btn4" Margin="" Background="{Binding Source={StaticResource ResourceKey=DataSource}, Path=ColorName}">
  Is bound to red</Button>

依赖属性对属性值继承的支持

MainWindows.xaml.cs

     private double fontSize = ;

        public MainWindow()
{
InitializeComponent();
fontSize = this.FontSize;
} private void SetFontSize(object sender, RoutedEventArgs e)
{
this.FontSize = ;
} private void SetButtonFont(object sender, RoutedEventArgs e)
{
this.btn6.FontSize = ;
} private void ResetFontSize(object sender, RoutedEventArgs e)
{
this.FontSize = fontSize;
this.btn6.FontSize = fontSize;
}

MainWindows.xaml

<Button Grid.Row="" Grid.Column="" Name="btn5" Margin="" Click="SetFontSize">Set the window font</Button>
<Button Grid.Row="" Grid.Column="" Name="btn6" Margin="" Click="SetButtonFont">Set the button font</Button>
<Button Grid.Row="" Grid.Column="" Name="btn7" Margin="" Click="ResetFontSize">Reset the font</Button>

依赖属性设置的优先级

优先级按从高到低排序:

  属性系统强制转换

  动画

  本地值

  模板属性

  隐式样式

  样式触发器

  模板触发器

  样式

  继承

  默认值

附加属性

在WPF里最典型的附加属性就是各种布局中的属性,Grid.Row DockPanel.Dock等,方便处理布局的问题。

附加属性实质是依赖属性,与普通的依赖属性相比有以下不同

注册方式不同,通过Get\Set实现属性封装,没有普通的.Net属性

依赖属性的安全性

https://msdn.microsoft.com/zh-cn/library/cc903923(v=vs.95).aspx

To be continue...

WPF学习之路(三) 属性与依赖的更多相关文章

  1. WPF学习之路初识

    WPF学习之路初识   WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...

  2. 学习之路三十九:新手学习 - Windows API

    来到了新公司,一开始就要做个程序去获取另外一个程序里的数据,哇,挑战性很大. 经过两周的学习,终于搞定,主要还是对Windows API有了更多的了解. 文中所有的消息常量,API,结构体都整理出来了 ...

  3. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  4. 【WPF学习】第三十六章 样式基础

    前面三章介绍了WPF资源系统,使用资源可在一个地方定义对象而在整个标记中重用他们.尽管可使用资源存储各种对象,但使用资源最常见的原因之一是通过他们的保存样式. 样式是可应用于元素的属性值集合.WPF样 ...

  5. Redis——学习之路三(初识redis config配置)

    我们先看看config 默认情况下系统是怎么配置的.在命令行中输入 config get *(如图) 默认情况下有61配置信息,每一个命令占两行,第一行为配置名称信息,第二行为配置的具体信息.     ...

  6. WPF学习之路(十四)样式和模板

    样式 实例: <Window.Resources> <Style x:Key="BtnStyle"> <Setter Property=" ...

  7. 【WPF学习】第三十三章 高级命令

    前面两章介绍了命令的基本内容,可考虑一些更复杂的实现了.接下来介绍如何使用自己的命令,根据目标以不同方式处理相同的命令以及使用命令参数,还将讨论如何支持基本的撤销特性. 一.自定义命令 在5个命令类( ...

  8. 【WPF学习】第三十二章 执行命令

    前面章节已经对命令进行了深入分析,分析了基类和接口以及WPF提供的命令库.但尚未例举任何使用这些命令的例子. 如前所述,RoutedUICommand类没有任何硬编码的功能,而是只表达命令,为触发命令 ...

  9. 【WPF学习】第三十四章 资源基础

    WPF允许在代码中以及在标记中的各个位置定义资源(和特定的控件.窗口一起定义,或在整个应用程序中定义). 资源具有许多重要的优点,如下所述: 高效.可以通过资源定义对象,并在标记中的多个地方使用.这会 ...

随机推荐

  1. [前端]使用JQuery UI Layout Plug-in布局 - wolfy

    引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...

  2. Bootstrap学习笔记系列2-------Bootstrap简单表格处理

    标签 <table> 为表格添加基础样式 <thead> 表格标题行的容器元素,用来识别列 <tbody> 表格主提中的表格行的容器元素 <tr> 单行 ...

  3. C#的变迁史 - C# 5.0 之其他增强篇

    1. 内置zip压缩与解压 Zip是最为常用的文件压缩格式之一,也被几乎所有操作系统支持.在之前,使用程序去进行zip压缩和解压要靠第三方组件去支持,这一点在.NET4.5中已有所改观,Zip压缩和解 ...

  4. ASP.NET MVC 网站开发总结(七)——C#操作图片:多张图的拼接(旋转)

    其实用C#来操作图片的拼接就是在用Graphic画图.个人感觉还是挺有趣的,各种类库提供了丰富多彩的功能. 源代码(移植到一个简单的C#程序中,并没有放在ASP.NET项目中): using Syst ...

  5. 记一次ckeditor上传图片到服务器问题

    package com.util;import java.io.IOException; import java.io.PrintWriter; import java.util.List;impor ...

  6. How to DEBUG a trigger or procedure

    DEBUGGING STORED PROCEDURES Over the past several weeks, we’ve been working on debugging a stored pr ...

  7. Hadoop笔记系列 一 用Hadoop进行分布式数据处理(1)

    学习资料参考地址: 1.http://blog.csdn.net/zhoudaxia/article/details/8801769 1.先说说什么是Hadoop? 个人理解:一个分布式文件存储系统+ ...

  8. C#实现通过Gzip来对数据进行压缩和解压

    C#实现通过Gzip来对数据进行压缩和解压 internal static byte[] Compress(byte[] data) { using (var compressedStream = n ...

  9. GJM : Unity3D 常用网络框架与实战解析 【笔记】

    Unity常用网络框架与实战解析 1.Http协议          Http协议                  存在TCP 之上 有时候 TLS\SSL 之上 默认端口80 https 默认端口 ...

  10. 使用CTE解决复杂查询的问题

    最近,同事需要从数个表中查询用户的业务和报告数据,写了一个SQL语句,查询比较慢: Select S.Name, S.AccountantCode, ( Select COUNT(*) from ( ...