Wpf 依赖属性 附加属性

一、依赖属性

依赖属性就是自己自己没有值,通过Binding从数据源获得值,就是依赖在别人身上,拥有依赖属性的对象称为依赖对象。

几种应用依赖属性的场景:

1. 希望可在样式中设置属性。

2. 希望属性支持数据绑定。

3. 希望可使用动态资源引用设置属性。

4. 希望从元素树中的父元素自动继承属性值。

5. 希望属性可进行动画处理。

6. 希望属性系统在属性系统、环境或用户执行的操作或者读取并使用样式更改了属性以前的值时报告。

7. 希望使用已建立的、WPF 进程也使用的元数据约定,例如报告更改属性值时是否要求布局系统重新编写元素的可视化对象。

依赖对象创建时并不包含存储数据空间。WPF中必须使用依赖对象作为依赖属性的宿主。

 

声明和使用依赖属性

提示:创建依赖属性时可以使用代码片段propdp

 1        <Window x:Class="DeepXAML.MainWindow"
 2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4              xmlns:local="clr-namespace:DeepXAML"       
 5              xmlns:sys="clr-namespace:System;assembly=mscorlib"
 6              Title="MainWindow" Height="250" Width="450">
 7          <StackPanel x:Name="stackPanel">
 8              <TextBox x:Name="textBox1" Margin="10"  Text="{Binding Path=Title}"></TextBox>
 9              <TextBox x:Name="textBox2" Margin="10" Text="{Binding Path=Title}"></TextBox>
10          </StackPanel>
11      </Window>    
     using System;
     using System.Collections.Generic;
     using System.Windows;
     using System.Windows.Data;
     using System.Windows.Documents;
       
     namespace DeepXAML
     {
        public partial class MainWindow : Window
         {
             public MainWindow()
             {
                 InitializeComponent();
                 Game game = new Game { Title = "WarCraft" };
                 this.stackPanel.DataContext = game;
             }
         }
       
         public class Game:DependencyObject
         {
             public string Title
             {
                get { return (string)GetValue(NameProperty); }
                 set { SetValue(NameProperty, value); }
             }
             // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
             public static readonly DependencyProperty NameProperty =
                 DependencyProperty.Register("Title", typeof(string), typeof(Game));        
         }  
     }

DependencyProperty.Register方法解释

a.第一个参数指明以哪个CLR属性作为依赖属性的包装

b. 第二参数指明依赖属性存期什么类型的值

c. 此依赖属性宿主的类型。

依赖属性的值存在哪里?

在WPF运行时,维护了一个全局的Hashtable存取依赖属性的值。

 

二、附加属性

附加属性就是自己没有这个属性,在某些上下文中需要就被附加上去。比如TextBox的Grid.Row属性,如果我们定义TextBox类时定义一个Row属性是没有意义的,因为我们并不知道一定会放在Grid里,这样就造成了浪费。

 1 01     <Window x:Class="DeepXAML.MainWindow"
 2 02             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3 03             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4 04             xmlns:local="clr-namespace:DeepXAML"       
 5 05             xmlns:sys="clr-namespace:System;assembly=mscorlib"
 6 06             Title="MainWindow" Height="250" Width="450">
 7 07         <Grid>
 8 08             <Grid.RowDefinitions>
 9 09                 <RowDefinition Height="2*"></RowDefinition>
10 10                 <RowDefinition></RowDefinition>
11 11             </Grid.RowDefinitions>
12 12             <TextBox Grid.Row="0"></TextBox>
13 13             <TextBox Grid.Row="1"></TextBox>
14 14         </Grid>
15 15     </Window>

实际上在编译后,Grid.Row会在Grid的类里生成SetRow方法,Grid来调整控件的控制。也就是附加属性会生成方法,不会占用内存空间。

 1      public class GameAP : DependencyObject
 2      {
 3          public static int GetName(DependencyObject obj)
 4          {
 5              return (int)obj.GetValue(NameProperty);
 6          }
 7        
 8          public static void SetName(DependencyObject obj, int value)
 9          {
10              obj.SetValue(NameProperty, value);
11          }
12        
13          // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
14          public static readonly DependencyProperty NameProperty =
15              DependencyProperty.RegisterAttached("Name", typeof(int), typeof(GameAP), new UIPropertyMetadata(0));
16            
17      }

 

posted @ 2013-04-24 10:37  蘑菇mr  阅读(297)  评论(0编辑  收藏  举报