照书学WPF之 Dependency Property 1
簡介
Dependency Property: 它們依賴一些其他的property和外在的影響,引起自身的變化.
如: WPF框架的編程經常和界面打交道,經常遇到的一個情況是某個屬性的值的變化會影響到多個其他對象。比如當一個Button的改變大小超過了它的容器,他的容器應該自動調整大小。于是我們考慮在每個屬性的set方法中觸發一些事件,但很快我們發現現有的功能很難滿足我們的需求,至少不能簡潔漂亮的滿足這些需求。
?定義示例: public static readonly DependencyProperty FontSizePropery;
1.簡單的element tree
?通過簡單的例子來理解"沿襲"(不能說是繼承)
窗口構造函數將Font size 初使化為16設備無關的單位. 所有的BUTTON都會沿襲這個設置(都是WINDOW的孩子)
?1using?System;
?2using?System.Collections.Generic;
?3using?System.Text;
?4using?System.Windows;
?5using?System.Windows.Controls;
?6using?System.Windows.Documents;
?7
?8namespace?DockAndGrid
?9{
10????public?class?SetFontSizeProperty?:?Window
11????{
12????????[STAThread]
13????????public?static?void?Main()
14????????{
15????????????Application?app?=?new?Application();
16????????????app.Run(new?SetFontSizeProperty());
17????????}
18
19????????public?SetFontSizeProperty()
20????????{
21????????????Title?=?"Set?size?property";
22????????????SizeToContent?=?SizeToContent.WidthAndHeight;
23????????????ResizeMode?=?ResizeMode.CanMinimize;
24????????????FontSize?=?16;
25????????????Double[]?fntsizes?=?{?8,16,32};
26
27????????????Grid?grid?=?new?Grid();
28????????????Content?=?grid;
29
30????????????for?(int?i?=?0;?i?<?2;?i++)
31????????????{
32????????????????RowDefinition?row?=?new?RowDefinition();
33????????????????row.Height?=?GridLength.Auto;
34????????????????grid.RowDefinitions.Add(row);
35????????????}
36????????????for?(int?i?=?0;?i?<?fntsizes.Length;?i++)
37????????????{
38????????????????ColumnDefinition?col?=?new?ColumnDefinition();
39????????????????col.Width?=?GridLength.Auto;
40????????????????grid.ColumnDefinitions.Add(col);
41????????????}
42
43????????????for?(int?i?=?0;?i?<?fntsizes.Length;?i++)
44????????????{
45????????????????Button?btn?=?new?Button();
46????????????????btn.Content?=??new?TextBlock(new?Run("Set?Window?FontSize?to?"?+?fntsizes[i]));
47????????????????btn.Tag?=?fntsizes[i];
48????????????????btn.HorizontalAlignment?=?HorizontalAlignment.Center;
49????????????????btn.VerticalAlignment?=?VerticalAlignment.Center;
50????????????????btn.Click?+=?this.WindowFontSizeOnClick;
51????????????????grid.Children.Add(btn);
52????????????????Grid.SetRow(btn,?0);
53????????????????Grid.SetColumn(btn,?i);
54
55????????????????btn?=?new?Button();
56????????????????btn.Content?=?new?TextBlock(new?Run("Set?Button?FontSize?to?"?+?fntsizes[i]));
57????????????????btn.Tag?=?fntsizes[i];
58????????????????btn.HorizontalAlignment?=?HorizontalAlignment.Center;
59????????????????btn.VerticalAlignment?=?VerticalAlignment.Center;
60????????????????btn.Click?+=?this.ButtonFontSizeOnClick;
61????????????????grid.Children.Add(btn);
62????????????????Grid.SetRow(btn,?1);
63????????????????Grid.SetColumn(btn,?i);
64????????????}
65????????}
66
67????????void?WindowFontSizeOnClick(object?sender,?RoutedEventArgs?args)
68????????{
69????????????Button?btn?=?args.Source?as?Button;
70????????????FontSize?=?(double)btn.Tag;
71????????}
72
73????????void?ButtonFontSizeOnClick(object?sender,?RoutedEventArgs?args)
74????????{
75????????????Button?btn?=?args.Source?as?Button;
76????????????btn.FontSize?=?(double)btn.Tag;
77????????}
78
79????}
80}
81
?element tree是我們能看到的(實際)能看到的視覺對像, 是對logic tree的簡單處理,更容易理解程序 (font size如何被處理的), 在此程序中為window->grid->(button collection)->(textblock collection)
特點:樹中較低的對像"沿襲"父親的property,然而如果某個對像有明確的設定自己的font size,那么就不用沿襲父親的這個property
注: Grid根本就沒有fontsize property,但依然可以沿襲(內部機制,不是很明白)
?property set priority: 對像自己的設定> 沿襲來的值 > 默認值
?
2. dependecy property 示例
在WPF中,dependency property的使用,允許以一般的方式自動進行大部分的通知.
定義:? public static readonly DependencyProperty SpaceProperty;
它是public and static, 此成員只與類相關,而非與對象相關聯.
注冊:? SpaceProperty =? DependencyProperty.Register.("Name", typeof(datatype), typeof(ownertype));
實際上這個property不可以簡單的這么完成,還要須要元數據等元素,并且由于是靜態的成員,必須在靜態構造函數中初使化
static SpaceButton()
??????? {
??????????? //define metadata
??????????? FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
??????????? metadata.DefaultValue = 1;
??????????? metadata.AffectsMeasure = true;
??????????? metadata.Inherits = true;
??????????? metadata.PropertyChangedCallback += OnSpacePropertyChanged;
??????????? //Register dependency property
??????????? SpaceProperty = DependencyProperty.Register("Space", typeof(int), typeof(SpaceButton),
??????????????? metadata, ValidateSpaceValue);
??????? }
ValidateSpaceValue: 檢驗值是否合法
AffectsMeasure: 影響控件的尺寸調整.
使用: .Net CLR wrapper
public int Space
??????? {
??????????? set { SetValue(SpaceProperty, value); }
??????????? get { return (int)GetValue(SpaceProperty); }
??????? }
注意:即使傳到SetValue and GetValue方法內當參數的property的對象是靜態的,而這兩個方法卻是實例方法, 它們的值的設定和取得都和特定的實例有關, 這個property維持目前的值,且處理所有的日常事務.
完整示例:
Code??1using?System;
??2using?System.Collections.Generic;
??3using?System.Linq;
??4using?System.Text;
??5using?System.Windows;
??6using?System.Windows.Controls;
??7using?System.Windows.Media;
??8using?System.Windows.Input;
??9
?10namespace?DependencyProperty1
?11{
?12????class?SpaceButton?:?Button
?13????{
?14????????string?txt;
?15????????public?string?Text
?16????????{
?17????????????get?{?return?txt;?}
?18????????????set
?19????????????{
?20????????????????txt?=?value;
?21????????????????Content?=?SpaceOutText(txt);
?22????????????}
?23????????}
?24
?25????????public?static?readonly?DependencyProperty?SpaceProperty;
?26????????public?int?Space
?27????????{
?28????????????set?{?SetValue(SpaceProperty,?value);?}
?29????????????get?{?return?(int)GetValue(SpaceProperty);?}
?30????????}
?31
?32????????static?SpaceButton()
?33????????{
?34????????????//define?metadata
?35????????????FrameworkPropertyMetadata?metadata?=?new?FrameworkPropertyMetadata();
?36????????????metadata.DefaultValue?=?1;
?37????????????metadata.AffectsMeasure?=?true;
?38????????????metadata.Inherits?=?true;
?39????????????metadata.PropertyChangedCallback?+=?OnSpacePropertyChanged;
?40
?41????????????//Register?dependency?property
?42????????????SpaceProperty?=?DependencyProperty.Register("Space",?typeof(int),?typeof(SpaceButton),
?43????????????????metadata,?ValidateSpaceValue);
?44????????}
?45
?46????????static?bool?ValidateSpaceValue(object?obj)
?47????????{
?48????????????int?i?=?(int)obj;
?49????????????return?i?>=?0;
?50????????}
?51
?52????????//Recall?method,?when?the?property?change
?53????????static?void?OnSpacePropertyChanged(DependencyObject?obj,?DependencyPropertyChangedEventArgs?args)
?54????????{
?55????????????SpaceButton?btn?=?obj?as?SpaceButton;
?56????????????btn.Content?=?btn.SpaceOutText(btn.txt);
?57????????}
?58
?59????????private?string?SpaceOutText(string?str)
?60????????{
?61????????????if?(str?==?null)
?62????????????????return?null;
?63
?64????????????StringBuilder?sb?=?new?StringBuilder();
?65????????????foreach?(char?ch?in?str)
?66????????????{
?67????????????????sb.Append(ch?+?new?string('?',?Space));
?68????????????}
?69????????????return?sb.ToString();
?70????????}
?71????}
?72
?73????public?class?SpaceWindow?:?Window
?74????{
?75????????public?static?readonly?DependencyProperty?SpaceProperty;
?76????????public?int?Space
?77????????{
?78????????????set?{?SetValue(SpaceProperty,?value);?}
?79????????????get?{?return?(int)GetValue(SpaceProperty);?}
?80????????}
?81
?82????????static?SpaceWindow()
?83????????{
?84????????????FrameworkPropertyMetadata?metadata?=?new?FrameworkPropertyMetadata();
?85????????????metadata.Inherits?=?true;
?86
?87????????????//Add?owner?to?SpacePropery?and?override?metadata
?88????????????SpaceProperty?=?SpaceButton.SpaceProperty.AddOwner(typeof(SpaceWindow));
?89????????????SpaceProperty.OverrideMetadata(typeof(SpaceWindow),?metadata);
?90????????}
?91
?92????}
?93
?94????public?class?SetSpaceProperty?:?SpaceWindow
?95????{
?96????????[STAThread]
?97????????public?static?void?Main()
?98????????{
?99????????????Application?app?=?new?Application();
100????????????app.Run(new?SetSpaceProperty());
101????????}
102
103????????public?SetSpaceProperty()
104????????{
105????????????Title?=?"Set?Space?property";
106
107????????????SizeToContent?=?SizeToContent.WidthAndHeight;
108????????????ResizeMode?=?ResizeMode.CanMinimize;
109????????????int[]?iSpace?=?{?0,1,2};
110
111????????????Grid?grid?=?new?Grid();
112????????????Content?=?grid;
113
114????????????for?(int?i?=?0;?i?<?2;?i++)
115????????????{
116????????????????RowDefinition?row?=?new?RowDefinition();
117????????????????row.Height?=?GridLength.Auto;
118????????????????grid.RowDefinitions.Add(row);
119????????????}
120
121????????????for?(int?i?=?0;?i?<?iSpace.Length;?i++)
122????????????{
123????????????????ColumnDefinition?col?=?new?ColumnDefinition();
124????????????????col.Width?=?GridLength.Auto;
125????????????????grid.ColumnDefinitions.Add(col);
126????????????}
127
128????????????for?(int?i?=?0;?i?<?iSpace.Length;?i++)
129????????????{
130????????????????SpaceButton?btn?=?new?SpaceButton();
131????????????????btn.Text?=?"Set?window?space?to?"?+?iSpace[i];
132????????????????btn.Tag?=?iSpace[i];
133????????????????btn.HorizontalAlignment?=?HorizontalAlignment.Center;
134????????????????btn.VerticalAlignment?=?VerticalAlignment.Center;
135????????????????btn.Click?+=?this.WindowPropertyOnClick;
136????????????????grid.Children.Add(btn);
137????????????????Grid.SetRow(btn,?0);
138????????????????Grid.SetColumn(btn,?i);
139
140????????????????btn?=?new?SpaceButton();
141????????????????btn.Text?=?"Set?button?spae?to?"?+?iSpace[i];
142????????????????btn.Tag?=?iSpace[i];
143????????????????btn.HorizontalAlignment?=?HorizontalAlignment.Center;
144????????????????btn.VerticalAlignment?=?VerticalAlignment.Center;
145????????????????btn.Click?+=?this.ButtonPropertyOnClick;
146????????????????grid.Children.Add(btn);
147????????????????Grid.SetRow(btn,?1);
148????????????????Grid.SetColumn(btn,?i);
149
150????????????}
151????????}
152
153????????void?WindowPropertyOnClick(object?sender,?RoutedEventArgs?args)
154????????{
155????????????SpaceButton?btn?=?args.Source?as?SpaceButton;
156????????????Space?=?(int)btn.Tag;
157????????}
158
159????????void?ButtonPropertyOnClick(object?sender,?RoutedEventArgs?args)
160????????{
161????????????SpaceButton?btn?=?args.Source?as?SpaceButton;
162????????????btn.Space?=?(int)btn.Tag;
163????????}
164
165
166????}
167}
168
[Question]Logical tree, Visual tree, will trace it in the next
?
?
轉載于:https://www.cnblogs.com/refeiner/archive/2009/04/18/1437623.html
總結
以上是生活随笔為你收集整理的照书学WPF之 Dependency Property 1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSP中javaBean和Servlet
- 下一篇: 什么时候使用webservice1