依赖属性与一般属性相比,提供了对资源引用、样式、动画、数据绑定、属性值继承、元数据重载以及WPF设计器的继承支持功能的支持。
下面的这个Demo来自《葵花宝典--WPF自学手册》。
1、MainWindow.xaml
1 <Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:sys="clr-namespace:System;assembly=mscorlib" 5 xmlns:local="clr-namespace:WpfApplication1" 6 Title="MainWindow" Height="350" Width="525"> 7 <Window.Resources> 8 <SolidColorBrush x:Key="MyBrush" Color="Gold"/> 9 <Style x:Key="GreenButtonStyle"> 10 <Setter Property="Control.Background" Value="Green"/> 11 </Style> 12 <local:BindingData x:Key="myDataSource"> 13 14 </local:BindingData> 15 </Window.Resources> 16 <Grid> 17 <Grid.RowDefinitions> 18 <RowDefinition/> 19 <RowDefinition/> 20 <RowDefinition/> 21 </Grid.RowDefinitions> 22 <Grid.ColumnDefinitions> 23 <ColumnDefinition/> 24 <ColumnDefinition/> 25 <ColumnDefinition/> 26 <ColumnDefinition/> 27 </Grid.ColumnDefinitions> 28 29 <Label HorizontalAlignment="Center" VerticalAlignment="Center"> 30 资源支持 31 </Label> 32 <!--金色按钮的背景属性引用了画刷资源--> 33 <Button Grid.Row="0" Grid.Column="1" Name="resourceBtn" Background="{DynamicResource MyBrush}"> 34 金色按钮 35 </Button> 36 <Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"> 37 样式支持 38 </Label> 39 <!--绿色按钮的Style属性引用了样式资源 --> 40 <Button Grid.Row="0" Grid.Column="3" Name="typeBtn" Style="{StaticResource GreenButtonStyle}"> 41 绿色按钮 42 </Button> 43 <Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"> 44 动画支持 45 </Label> 46 <!--动画按钮的背景属性支持动画--> 47 <Button Grid.Row="1" Grid.Column="1" Name="animationBtn"> 48 动画按钮 49 <Button.Background> 50 <SolidColorBrush x:Name="AnimBrush"/> 51 </Button.Background> 52 <Button.Triggers> 53 <EventTrigger RoutedEvent="Button.Loaded"> 54 <BeginStoryboard> 55 <Storyboard> 56 <ColorAnimation Storyboard.TargetName="AnimBrush" 57 Storyboard.TargetProperty="(SolidColorBrush.Color)" 58 From="Red" To="Green" Duration="0:0:5" 59 AutoReverse="True" RepeatBehavior="Forever"/> 60 </Storyboard> 61 </BeginStoryboard> 62 </EventTrigger> 63 </Button.Triggers> 64 </Button> 65 <Label Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"> 66 数据绑定支持 67 </Label> 68 <!--按钮对绑定数据的支持--> 69 <Button Grid.Row="1" Grid.Column="3" Name="bindingBtn" Background="{Binding Source={StaticResource myDataSource},Path=ColorName}"> 70 被绑定为红色! 71 </Button> 72 <!--依赖属性改变相应元素的字体大小--> 73 <Label Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"> 74 属性继承支持 75 </Label> 76 <Button Grid.Row="2" Grid.Column="1" Name="FontSizeWinBtn" Click="FontSizeWinBtn_Click"> 77 设置窗口字体:16 78 </Button> 79 <Button Grid.Row="2" Grid.Column="2" Name="FontSizeBtn" Click="FontSizeBtn_Click"> 80 设置按钮字体:8 81 </Button> 82 <Button Grid.Row="2" Grid.Column="3" Name="ResetFontSizeBtn" Click="ResetFontSizeBtn_Click"> 83 重置窗口字体:12 84 </Button> 85 </Grid> 86 87 </Window>
2、BindingData.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace WpfApplication1 8 { 9 class BindingData 10 { 11 public BindingData() 12 { 13 ColorName = "Red"; 14 } 15 16 private string name = "Red"; 17 public string ColorName 18 { 19 get { return name; } 20 set 21 { 22 name = value; 23 24 } 25 } 26 } 27 }
3、MainWindow.xaml.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace WpfApplication1 17 { 18 /// <summary> 19 /// Interaction logic for MainWindow.xaml 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 private double _oldFontSize = 0; 24 public MainWindow() 25 { 26 InitializeComponent(); 27 28 _oldFontSize = FontSize; 29 } 30 //改变窗体所有元素的字体大小 31 private void FontSizeWinBtn_Click(object sender, RoutedEventArgs e) 32 { 33 FontSize = 16; 34 } 35 //仅改变该按钮的字体大小 36 private void FontSizeBtn_Click(object sender, RoutedEventArgs e) 37 { 38 this.FontSizeBtn.FontSize = 8; 39 } 40 //恢复字体大小 41 private void ResetFontSizeBtn_Click(object sender, RoutedEventArgs e) 42 { 43 FontSize = _oldFontSize; 44 this.FontSizeBtn.FontSize = _oldFontSize; 45 } 46 47 } 48 }
4、运行结果
注意,
在第一次点击“设置窗口字体:16”的按钮时,窗口内所有字体都变成了16号大小的字体,因为这时,窗口内的元素都没有设置过FontSize这个属性,所以继承了窗口的FontSize属性值;
点击"设置按钮字体:8"按钮时,只有该按钮的字体变成8号,因为事件处理函数中只对它的FontSize属性赋值(this.FoniSizeBtn.FontSize=8;)。
之后,再次点击“设置窗口字体:16”的按钮时,"设置按钮字体:8"按钮的字体不会随之变成16号字体,因为该按钮的FontSize属性已经赋值过了,它就不再继承使用窗口的FontSize属性值了。