[Windbg調試WPF的依賴屬性](http://blog.csdn.net/muzizongheng/article/details/46821459)中提到了wpf的DependencyObject中DependencyProperty是如何調試查看的。
從中我們看出DO(DependencyObject)與 DP(DependencyProperty)一些內部實現。
**這篇文章我們就從源碼入手, 讓大家了解下依賴對象中依賴屬性的值的獲取和賦值。**
我們先看個DP注冊的例子:
~~~
public class MyStateControl : ButtonBase
{
public MyStateControl() : base() { }
public Boolean State
{
get { return (Boolean)this.GetValue(StateProperty); }
set { this.SetValue(StateProperty, value); }
}
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
"State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));
}
~~~
上述Code中MyStateControl是DO,StateProperty是DP
**1.**
**當MyStateControl進行初始化, 首先會執行StateProperty, 因為它是靜態字段。從而執行DependencyProperty.Register方法。**
**
**
**2.**
**這個方法內部調用了DP的構造方法, Code如下:**
?// Create property
?????????? DependencyProperty dp =?new?DependencyProperty(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
**3.**
**DP的構造方法如下:**
?private?DependencyProperty(?string?name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
?????? {
?????????? _name = name;
?????????? _propertyType = propertyType;
?????????? _ownerType = ownerType;
?????????? _defaultMetadata = defaultMetadata;
?????????? _validateValueCallback = validateValueCallback;
?????????? Flags packedData;
???????????lock?(Synchronized)
?????????? {
???????????????packedData = (Flags) GetUniqueGlobalIndex(ownerType, name);
?????????????? RegisteredPropertyList.Add(?this);
?????????? }
???????????if?(propertyType.IsValueType)
?????????? {
?????????????? packedData |= Flags.IsValueType;
?????????? }
???????????if?(propertyType ==?typeof?(object))
?????????? {
?????????????? packedData |= Flags.IsObjectType;
?????????? }
???????????if?(typeof?(Freezable).IsAssignableFrom(propertyType))
?????????? {
?????????????? packedData |= Flags.IsFreezableType;
?????????? }
???????????if?(propertyType ==?typeof?(string))
?????????? {
?????????????? packedData |= Flags.IsStringType;
?????????? }
???????????_packedData = packedData;
?????? }
**4.**
**第3點的Code中我們可以看到packedData初始值是?(Flags) GetUniqueGlobalIndex(ownerType, name);**
**GetUniqueGlobalIndex其實是DP的私有靜態變量GlobalIndexCount++得到的。**
下來這段代碼可以看出:
[Flags]
???????private?enum?Flags :?int
?????? {
?????????? GlobalIndexMask?????????????????????????? = 0x0000FFFF,
?????????? IsValueType?????????????????????????????? = 0x00010000,
?????????? IsFreezableType?????????????????????????? = 0x00020000,
?????????? IsStringType????????????????????????????? = 0x00040000,
?????????? IsPotentiallyInherited??????????????????? = 0x00080000,
?????????? IsDefaultValueChanged???????????????????? = 0x00100000,
?????????? IsPotentiallyUsingDefaultValueFactory???? = 0x00200000,
?????????? IsObjectType????????????????????????????? = 0x00400000,
???????????// 0xFF800000?? free bits
?????? }
public?int?GlobalIndex
?????? {
???????????get?{?return?(int) (_packedData & Flags.GlobalIndexMask); }
?????? }
_packedData的低4位即代表了StateProperty在整個DP數組RegisteredPropertyList中的索引。
**5.**
**我們在構造里看到_packedData成員變量, 還記得我們[“windbg如何調試依賴屬性”](http://blog.csdn.net/muzizongheng/article/details/46821459)用到了它嗎?**
**我們用?.formats?命令轉換去掉_packedData高位得到了DP在DO中的存儲索引。**
**通過第4和第5點, 想必大家已經對DP注冊有了了解**
**
**
**接下來我們再看下DO中如何獲取DP值,以及如何設置DP值。**
**6.**
**首先我們說下DO設置DP,Code類似:**
~~~
set { this.SetValue(StateProperty, value); }
~~~
~~~
~~~
~~~
可以看到我們通過DO的SetValue來給DP設置值。
~~~
~~~
~~~
**7.**
**SetValue內部實現如下:**
private void SetValueCommon(
? ? ? ? ? DependencyProperty ?dp,
? ? ? ? ? object ? ? ? ? ? ? ?value,
? ? ? ? ? PropertyMetadata ? ?metadata,
? ? ? ? ? bool ? ? ? ? ? ? ? ?coerceWithDeferredReference,
? ? ? ? ? bool ? ? ? ? ? ? ? ?coerceWithCurrentValue,
? ? ? ? ? OperationType ? ? ? operationType,
? ? ? ? ? bool ? ? ? ? ? ? ? ?isInternal)
? ? ? {
? ? ? ? ?。。。。。。
? ? ? ??? EntryIndex entryIndex = LookupEntry(dp.GlobalIndex);
? ? ? ? 。。。。。。
**大家可以看到DO根據DP的GlobalIndex在_effectiveValues數組中查找到EntryIndex, EntryIndex包含對應index和Value,如果沒有查到則在_effectiveValues中插入并返回index。**
**(有興趣可以看看LookupEntry的實現)**
**在數組中找到之后接下來就是往數組中賦值。代碼類似(真實比下面更復雜):**
if?(entryIndex.Found)
?????????????? {
?????????????????? newEntry =?_effectiveValues[entryIndex.Index];
?????????????? }
? ? ? ? ? ? ?
**然后調用UpdateEffectiveValue發送屬性更改通知。(還有其他一些Coerce相關代碼,暫且不述)**
**
// fire change notification
?????????????????? NotifyPropertyChange(
???????????????????????????new?DependencyPropertyChangedEventArgs(
?????????????????????????????????? dp,
?????????????????????????????????? metadata,
?????????????????????????????????? isAValueChange,
?????????????????????????????????? oldEntry,
?????????????????????????????????? newEntry,
?????????????????????????????????? operationType));
通過上面我們可以了解依賴對象中的依賴屬性的賦值實現, 我們接下來再看看取值。
**8.**
**DO獲取DP的值,Code類似:**
~~~
~~~
~~~
get { return (Boolean)this.GetValue(StateProperty); }
~~~
**9.?**
**GetValue內部實現如下:**
?public?object?GetValue(DependencyProperty dp)
?????? {
? ? ? ? ?。。。
???????????// Call Forwarded
???????????return?GetValueEntry(
???????????????????LookupEntry(dp.GlobalIndex),
?????????????????? dp,
???????????????????null,
?????????????????? RequestFlags.FullyResolved).Value;
?????? }
可以看出是先找到DP的索引,然后接下來從_effectiveValues數組中找到對應的值。代碼類似如下:
entry = _effectiveValues[entryIndex.Index];
(當然其中也有一些值優先級的處理,從來獲取到正確的值)
**
**
**OK, 我們再回想下Windbg中查看依賴對象的依賴屬性的值的步驟, 1.得到依賴對象的值;2.得到依賴屬性的值;3得到依賴屬性的GlobalIndex;4.根據GlobalIndex去依賴對象中的_effectiveValues找到對應index的值。**
**是不是對DP和DO的一些實現更了解了呢?**
~~~
~~~
- 前言
- win32與WPF的混合編程
- WPF: 一個可以用StoryBoard動態改變Grid行寬/列高的類
- MFC中調用WPF教程
- Expression Blend操作: 使用behavior來控制Storyboard
- WPF DatePicker 的textbox的焦點
- WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事項
- WPF TreeView 后臺C#選中指定的Item, 需要遍歷
- WPF GridViewColumn Sort DataTemplate
- DataGridColum的bug
- WPF Get Multibinding Expression, Update Source,
- WPF 后臺觸發 Validate UI‘s Element
- WPF ValidationRule 觸發ErrorTemplate 的注意事項
- WPF DelegateCommand CanExecute
- WPF TextBox PreviewTextInput handle IME (chinese)
- No overload for 'OnStartup' matches delegate 'System.Windows.StartupEventHandler'
- WPF error: does not contain a static 'Main' method suitable for an entry point
- WPF GridView中的CellTemplate失效的原因
- DataGrid 顯示選中的item
- 如何得到WPF中控件綁定的EventTrigger
- 選中DataGrid的Cell而不是row
- ContextMenu的自定義
- 輸入框只能輸入英文
- TextBox的OnTextboxChanged事件里對Text重新賦值帶中文, 導致崩潰
- DataGrid當列寬超出當前寬度時,沒有數據也恒有滾動條
- wpf如何獲取control template里的元素
- Set connectionId threw an exception.
- WPF中Visible設為Collapse時,VisualTreeHelper.GetChildrenCount為0
- XAML 編碼規范 (思考)
- 如何為現有控件的DependencyProperty添加Value Changed事件?
- TreeView滾動TreeViewItem
- 為BindingList添加Sort
- WPF Background的設置有坑
- 自定義Panel中添加依賴屬性需要注意的問題
- TextBlock截斷字符顯示為....
- DataGrid 支持字符截斷顯示
- TreeView控件實踐
- WPF如何更改系統控件的默認高亮顏色 (Highlight brush)
- ViewModel中C# Property自動添加OnPropertyChanged處理的小工具, 以及相應Python知識點
- WPF中Xaml編譯正常而Designer Time時出錯的解決辦法
- 關于Snoop的用法
- wpf中為DataGrid添加checkbox支持多選全選
- WPF中DataGrid控件的過濾(Filter)性能分析及優化
- wpf控件提示Value ‘’ can not convert
- DropShadowEffect導致下拉框控件抖動
- 再論WPF中的UseLayoutRounding和SnapsToDevicePixels
- WPF案例:如何設計歷史記錄查看UI
- WPF案例:如何設計搜索框(自定義控件的原則和方法)
- WPF基本概念入門
- WPF開發中Designer和碼農之間的合作
- 聊聊WPF中的Dispatcher
- 聊聊WPF中字體的設置
- Bug:DataGridCell的顯示不完整
- WPF中ToolTip的自定義
- WPF中ItemsControl綁定到Google ProtocolBuffer的結構體時的性能問題
- TreeView的性能問題
- Xaml中string(字符串)常量的定義以及空格的處理
- 依賴屬性
- WPF中的CheckBox的_ (underscore / 下劃線)丟失
- WPF錯誤:必須使“Property”具有非 null 值。
- WPF中ItemsControl應用虛擬化時找到子元素的方法
- WPF毫秒級桌面時鐘的實現-C#中Hook(鉤子)的應用
- KB2464222導致IsNonIdempotentProperty方法找不見
- WPF中PreviewMouseDownEvent的系統處理:TabItem的PreviewMouseDown 事件彈框后不切換的問題調查
- WPF文字渲染相關的問題及解決
- wpf中的默認右鍵菜單中的復制、粘貼、剪貼等沒有本地化的解決方案
- WPF內部DeliverEvent讀鎖和PrivateAddListener寫鎖導致死鎖
- Windbg調試WPF的依賴屬性
- WPF 后臺Render線程崩潰, Exception from HRESULT: 0x88980406
- WPF中DependencyObject與DependencyProperty的源碼簡單剖析
- 禁用WPF中DataGrid默認的鼠標左鍵拖動多選行的效果
- wpf工程中在Xaml文件下添加多個cs文件
- ScrollViewer滾動到底來觸發加載數據的Behavior