依赖属性与一般属性相比,提供了对资源引用、样式、动画、数据绑定、属性值继承、元数据重载以及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:依赖属性的应用的更多相关文章

  1. WPF依赖属性详解

    WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency P ...

  2. WPF自学入门(五)WPF依赖属性

    在.NET中有事件也有属性,WPF中加入了路由事件,也加入了依赖属性.最近在写项目时还不知道WPF依赖属性是干什么用的,在使用依赖项属性的时候我都以为是在用.NET中的属性,但是确实上不是的,通过阅读 ...

  3. WPF依赖属性值源(BaseValueSource)

    原文:WPF依赖属性值源(BaseValueSource)   WPF依赖属性提供一个机制,可以获取依赖属性提供值的来源 其以BaseValueSource枚举表示 1.Default public ...

  4. WPF依赖属性(续)(3)依赖属性存储

    原文:WPF依赖属性(续)(3)依赖属性存储          在之前的两篇,很多朋友参与了讨论,也说明各位对WPF/SL计数的热情,对DP系统各抒已见,当然也出现了一些分歧. 以下简称DP为依赖属性 ...

  5. WPF依赖属性(续)(1)

    原文:WPF依赖属性(续)(1)                 之前有写过几篇文章,详细地介绍了依赖属性的基本使用方法,如果你不想了解其内部实现机制的话,那么通过那两篇文章的介绍,足以应付平时的应用 ...

  6. WPF依赖属性(续)(2)依赖属性与附加属性的区别

    原文:WPF依赖属性(续)(2)依赖属性与附加属性的区别        接上篇,感谢各位的评论,都是认为依赖属性的设计并不是为了节省内存,从大的方面而讲是如此.样式,数据绑定,动画样样都离不开它.这篇 ...

  7. 监听WPF依赖属性

    原文:监听WPF依赖属性 当我们使用依赖属性的时候,有时需要监听它的变化,这在写自定义控件的时候十分有用, 下面介绍一种简单的方法.   如下使用DependencyPropertyDescripto ...

  8. WPF依赖属性的正确学习方法

    前言 我在学习WPF的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...

  9. WPF 依赖属性前言

    WPF 依赖属性前言 ​ 在.net中,我们可以属性来获取或设置字段的值,不需要在编写额外的get和set方法,但这有一个前提,那就是需要在对象中拥有一个字段,才能在此字段的基础上获取或设置字段的值, ...

  10. WPF 依赖属性

    依赖属性,简单的说,在WPF控件应用过程中,界面上直接可以引用的属性 如:<Button Content="aaa"></Button> Content称为 ...

随机推荐

  1. Linux命令点滴积累

    1.批量删除当前目录及子目录中指定类型的文件: [root@localhost logs]# find ./ -name *.bak | xargs rm -rf [root@localhost lo ...

  2. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  3. C语言:链表实现的一个实例

    问题:写一个程序输入你一年看过的所有电影以及每部电影的各种信息(简化问题:每部电影只要求输入片名和评价) 链表实现: #include<stdio.h> #include<stdli ...

  4. Markdown的使用简介

    以前有摘抄过,然而onenote速度感人,现在又主要用Linux,所以在这里备份一下,好方便用 Linux下推荐remakeble软件,或者直接sublime text,再或者vim,反正我不会ema ...

  5. 个人作业—Week3

    博客阅读体会 阅读了十几位软件工程师前辈的博文,了解了前辈们作为一名软件工程师的成长经历,我有一些感触. 这十几位前辈们的经历有着很大的差别,有的科班出身,有的则完全自学成才.不同的经历使得前辈们看问 ...

  6. Get&Post登录

    #import "MJViewController.h" @interface MJViewController () @property (weak, nonatomic) IB ...

  7. SQL 操作语句

    SQL Server T-SQL高级查询 高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student; --all 查询所有 ...

  8. python学习2 lambda表达式

    学习C#的,对这个就特别熟悉了 lambda函数,它没有名字,只有参数和表达式: lambda  args: expression def func1(func,arg): return func(a ...

  9. JavaWeb学习笔记——JavaEE基础知识

  10. Android学习笔记——TableLayout

    该工程的功能是实现在一个activity中显示一个表格 以下代码是MainActivity.java中的代码 package com.example.tablelayout; import andro ...