类型是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. iOS的QuickTime Plugin

    当UIWebView播放视频时,可以看到view hierarchy里有FigPluginView的身影.这个类来自于QuickTime Plugin,plugin的路径为: /Application ...

  2. jquery可见性选择器(综合)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 【Java每日一题】20161202

    20161201问题解析请点击今日问题下方的"[Java每日一题]20161202"查看 package Dec2016; public class Ques1202 { publ ...

  4. Delphi iOS 开启文件共享 UIFileSharingEnabled

    Apple 在 iOS 提供了文件共享(FileSharing)功能,让 App 有一个对外窗口的目录,透过 iTunes 可以任意管理这个目录的文档内容(可拖入文档,也能将文档拖出备份). 如果 A ...

  5. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  6. Java基础复习笔记系列 三

    前几节都是基础中的基础,从第三讲的笔记开始,每次笔记针对Java的一个知识块儿.  Java异常处理 1.什么是异常? 异常是指运行期出的错误.比如说:除以一个0:数组越界:读取的文件不存在. 异常处 ...

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

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

  8. PHP内核探索之变量(3)- hash table

    在PHP中,除了zval, 另一个比较重要的数据结构非hash table莫属,例如我们最常见的数组,在底层便是hash table.除了数组,在线程安全(TSRM).GC.资源管理.Global变量 ...

  9. apache maven pom setting

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  10. android 6.0添加权限

        @Override    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissio ...