类型是DependencyProperty的属性是依赖属性
依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值。
为什么引入依赖属性:
WPF的设计思想是侧重属性胜于方法和事件。
实例
依赖属性对资源引用的支持
设置Button的Background为金色
APP.xaml
<Application.Resources> <SolidColorBrush x:Key="MyBrush" Color="Gold" /> </Application.Resources>
MainWindow.xaml
<Button Grid.Row="0" Grid.Column="1" Name="btn1" Margin="5" 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="0" Grid.Column="3" Name="btn2" Margin="5" Style="{StaticResource GreenButtonStyle}">Green Button</Button>
依赖属性对动画的支持
<Button Grid.Row="1" Grid.Column="1" Name="btn3" Margin="5"> <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="1" Grid.Column="3" Name="btn4" Margin="5" Background="{Binding Source={StaticResource ResourceKey=DataSource}, Path=ColorName}"> Is bound to red</Button>
依赖属性对属性值继承的支持
MainWindows.xaml.cs
private double fontSize = 0; public MainWindow() { InitializeComponent(); fontSize = this.FontSize; } private void SetFontSize(object sender, RoutedEventArgs e) { this.FontSize = 16; } private void SetButtonFont(object sender, RoutedEventArgs e) { this.btn6.FontSize = 8; } private void ResetFontSize(object sender, RoutedEventArgs e) { this.FontSize = fontSize; this.btn6.FontSize = fontSize; }
MainWindows.xaml
<Button Grid.Row="2" Grid.Column="1" Name="btn5" Margin="5" Click="SetFontSize">Set the window font</Button> <Button Grid.Row="2" Grid.Column="2" Name="btn6" Margin="5" Click="SetButtonFont">Set the button font</Button> <Button Grid.Row="2" Grid.Column="3" Name="btn7" Margin="5" 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...