4个文件:App.xmal ;MainWindow.xaml ;MainWindow.xaml.cs; BindingData.cs(类文件,验证对数据绑定的支持)
废话不说,直接代码:
App.xmal
<Application x:Class="TestWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWPF"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!--依赖属性 资源支持-->
<SolidColorBrush x:Key="MyBrush" Color="Gold"/>
<!--依赖属性 样式支持-->
<!--将按钮的背景色设为绿色-->
<Style x:Key="GreenButtonStyle">
<Setter Property="Control.Background" Value="Green"/>
</Style>
<!--数据绑定支持 引入一个.net对象的资源 (使用自定义类,要声明 xmlns:.... )-->
<local:BindingData x:Key="myDataSource"/>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWPF"
Title="依赖属性" Height="350" Width="525">
<Grid Name="Grid1" >
<!--3行4列-->
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!--资源支持 在App.xaml中设置-->
<Label VerticalAlignment="Center" HorizontalAlignment="Center">资源支持</Label>
<Button Grid.Row="0" Grid.Column="1" Name="resourceBtn" Margin="5"
Background="{DynamicResource MyBrush}">金色按钮</Button>
<!--样式支持-->
<Label Grid.Row="0" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center">样式支持</Label>
<Button Grid.Row="0" Grid.Column="3" Name="styleBtn" Padding="0" Margin="5"
Style="{StaticResource GreenButtonStyle}">绿色按钮</Button>
<!--动画支持-->
<Label Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center">动画支持</Label>
<Button Grid.Row="1" Grid.Column="1" Name="animtionBtn" Margin="5">
<Button.Background>
<SolidColorBrush x:Name="AnimBrush"/>
</Button.Background>
<!--创建触发器-->
<Button.Triggers>
<!--事件通道-->
<EventTrigger RoutedEvent="Button.Loaded">
<BeginStoryboard>
<Storyboard>
<!-- 1.ColorAnimation(改变对象的填充色调)
2.DoubleAnimation(改变对象的任意一个属性(double类型的))
3.PointAnimation(改变对象的X、Y值,使控件的位置变化一次)-->
<!--From 指定起始值-->
<!--To 指定结束值 ;若要指定相对于起始值的结束值,设置By属性(而不是 To )-->
<!-- Duration 动画执行一次持续的时间长度(时:分:秒)-->
<!-- AutoReverse 控制动画是否回放 True=回放-->
<!--RepeatBehavior 重复次数-->
<ColorAnimation Storyboard.TargetName="AnimBrush"
Storyboard.TargetProperty="Color"
From="Red"
To="Green"
Duration="0:0:3"
AutoReverse="True"
RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
动画按钮</Button>
<!--数据绑定支持-->
<Label Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="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" VerticalAlignment="Center" HorizontalAlignment="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>
<!--对元数据重载的支持-->
<!--
元数据:元数据是一种二进制信息,用以对 存储在公共语言运行库可移植可执行文件 (PE) 文件 或
存储在内存中的 程序进行描述。
依赖属性和普通.NET程序的区别之一是元数据对象。
元数据和依赖属性是一对一的关系。通过设置元数据对象,可以修改依赖属性的状态和行为。
一般用到的元数据类是PropertyMetaData、FrameWorkPropertyMetaData。前者是后者的基类。
一般元数据对象包含的类型信息如下:
(1)默认值。例:Background的默认值为红色
(2)引用回调函数。
PropertyChangedCallBack 在属性值发生改变时调用;
CoerceValueCallBack 用于限制属性值;例:进度条的值限制在最小值和最大值之间。
(3)如果是框架级别的一些属性,例width、Background,则会有标识告知WPF该属性的某些状态信息
这也是FrameWorkPropertyMetaData类型元数据的特点。例:本例中的FontSize
其中Inherits 为 True,意味着此属性具有此属性值继承的特性。
-->
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 TestWPF
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
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)
{
FontSize = 8;
}
private void ResetFontSizeBtn_Click(object sender, RoutedEventArgs e)
{
FontSize = _oldFontSize;
this.ResetFontSizeBtn.FontSize = _oldFontSize;
}
}
}
BindingData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestWPF
{
class BindingData
{
public BindingData() {
ColorName = "Red";
}
private string name = "Red";
public string ColorName {
get { return name; }
set { this.name = value; }
}
}
}