一、附加屬性的特點
1、特殊的依賴屬性
2、用於非定義該屬性的類 例如Grid面板的RowDefinition、ColumnDefinition、Canvas面板的Left、Right
DockPanel面板的Dock都是附加屬性。
二、附加屬性的定義
1、聲明數據屬性變量。 public static 的DependencyProperty類型的變量。
2、在屬性系統中進行注冊,使用DependencyProperty.RegisterAttached()方法來注冊,方法參數和注冊依賴屬性時Register()方法的參數一致。
3、調用靜態方法設置和獲取屬性值。通過調用DependencyObject的SetValue()和GetValue()方法來設置和獲取屬性的值。
兩個方法應該命名為SetPropertyName()方法和GetPropertyName()方法。
三、示例演示附加屬性
實現的功能,窗體字體的大小隨TextBox控件里面輸入的值的大小而改變。
1、新建WPF版的用戶控件,命名為“MyDependencyProperty”,在用戶控件里面添加TextBox和TextBlock控件
XAML代碼:
1 <UserControl x:Class="WPFDependencyProperty.MyDependencyProperty" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:p="clr-namespace:WPFDependencyProperty" 7 mc:Ignorable="d" 8 d:DesignHeight="300" d:DesignWidth="300"> 9 <Grid> 10 <StackPanel> 11 <TextBox p:MyDependencyProperty.MyAttachedFontSize="{Binding Path=Text, 12 RelativeSource={RelativeSource Mode=Self}}" ></TextBox> 13 <TextBlock>通過附加屬性修改FontSize的大小</TextBlock> 14 </StackPanel> 15 </Grid> 16 </UserControl>
設計界面:
2、、在MyDependencyProperty.xaml.cs文件里面添加附件屬性,附件屬性的名稱為MyAttachedFontSize,使用快捷方式創建附件屬性:輸入propa,連續按兩下Tab健。
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 WPFDependencyProperty 17 { 18 /// <summary> 19 /// MyDependencyProperty.xaml 的交互邏輯 20 /// </summary> 21 public partial class MyDependencyProperty : UserControl 22 { 23 public MyDependencyProperty() 24 { 25 InitializeComponent(); 26 } 27 28 29 30 public static int GetMyAttachedFontSize(DependencyObject obj) 31 { 32 return (int)obj.GetValue(MyAttachedFontSizeProperty); 33 } 34 35 public static void SetMyAttachedFontSize(DependencyObject obj, int value) 36 { 37 obj.SetValue(MyAttachedFontSizeProperty, value); 38 } 39 40 // Using a DependencyProperty as the backing store for MyAttachedFontSize. This enables animation, styling, binding, etc... 41 public static readonly DependencyProperty MyAttachedFontSizeProperty = 42 DependencyProperty.RegisterAttached("MyAttachedFontSize", typeof(int), typeof(MyDependencyProperty), 43 new PropertyMetadata((s, e) => 44 { 45 //獲取MyDependencyProperty用戶控件,s代表Textbox, 46 //MyDependencyProperty用戶控件是TextBox的父級的父級的父級控件 47 var mdp = (((s as FrameworkElement).Parent as FrameworkElement).Parent 48 as FrameworkElement).Parent as MyDependencyProperty; 49 //更改用戶控件的FontSize的值 50 if (mdp != null && e.NewValue != null) 51 { 52 var fontsize = 9; 53 int.TryParse(e.NewValue.ToString(), out fontsize); 54 mdp.FontSize = fontsize; 55 } 56 })); 57 58 59 } 60 }
3、在主界面測試附件屬性
1 <Window x:Class="WPFDependencyProperty.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:p="clr-namespace:WPFDependencyProperty" 5 Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen"> 6 <Grid > 7 <StackPanel> 8 <TextBlock>請輸入字體的大小</TextBlock> 9 <p:MyDependencyProperty></p:MyDependencyProperty> 10 </StackPanel> 11 </Grid> 12 </Window>
程序運行效果:
在TextBox里面輸入3:
在TextBox里面輸入30: