认识依赖属性:
按钮为例,宽度、背景色和字体大小等都是依赖属性。
WPF主要的设计思想之一是侧重属性胜于方法和事件,即如果属性能解决问题,则坚决不使用方法和事件。
具体来说依赖属性与以前的属性相比,提供了对资源引用、样式、动画、数据绑定、属性值继承、元数据重载及WPF设计器的集成支持功能的支持。
什么时候需要自定义一个依赖属性:
1)支持动态资源引用
2)希望在样式中使用
3)支持动画
4)支持数据绑定
5)支持属性值继承
6)发生改变时触发一系列行为
7)希望有自己的元数据
8)希望得到WPF设计器的支持,如在WPF属性窗口中直接修改其值
实现依赖属性必须满足以下条件:
1)该类必须继承自DependencyObject类,只有DependencyObject类才可以注册拥有依赖属性。
2)该类中必须定义一个public static readonly 成员变量,类型为DependencyProperty,如“public static readonly DependencyPropert IsDefaultProperty;”
3)该依赖属性名必须以“属性名+Property”命名,如Button的IsDefault属性,命名为“IsDefaultProperty“
4)必须调用DependencyProperty的注册方法在WPF属性系统中注册该依赖或者使用依赖属性的AddOwner方法。两种方法均返回一个DependencyProperty类型的标识并将其保存在定义的DependencyProperty成员变量中。
5)为依赖属性实现一个.NET属性包装器。
App.xaml代码如下:
<Application x:Class="animate.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:animate"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:BindingData x:Key="myDataSource"/>
<SolidColorBrush x:Key="MyBrush" Color="Gold"/>
<Style x:Key="GreenButtonStyle">
<Setter Property="Control.Background" Value="Green"/>
</Style>
</Application.Resources>
</Application>
MainWindow.xaml代码如下:
<Window x:Class="animate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Opacity="0.995">
<Grid Name="Grid1">
<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" Margin="5" Background="{DynamicResource MyBrush}">金色按钮</Button>
<!--样式支持-->
<Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center">样式支持</Label>
<Button Grid.Row="0" Grid.Column="3" Name="styleBtn" Margin="5" 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" 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>
动画按钮</Button>
<!--数据绑定支持-->
<Label Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center">数据绑定支持</Label>
<Button Grid.Row="1" Grid.Column="3" Name="BindingBtn" Margin="5" 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="ResetSizeWinBtn" Click="ResetSizeWinBtn_Click">重置字体</Button>
</Grid>
</Window>
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 animate
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
///
class BindingData
{
public BindingData() { ColorName = "Red"; }
private string name = "Red";
public string ColorName
{
get { return name; }
set
{
name = value;
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_oldFontSize = FontSize;
}
private double _oldFontSize = 0;
private void FontSizeWinBtn_Click(object sender, RoutedEventArgs e)
{
FontSize = 16;
}
private void FontSizeBtn_Click(object sender, RoutedEventArgs e)
{
this.FontSizeBtn.FontSize = 8;
}
private void ResetSizeWinBtn_Click(object sender, RoutedEventArgs e)
{
FontSize = _oldFontSize;
this.FontSizeBtn.FontSize = _oldFontSize;
}
}
}
效果图: