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; }
}
}
}