2010-08-17 82 views
5

我剛剛回答的問題over here,我說有PathGeneratedInternally標誌在WPF綁定中做了什麼?

{Binding Path=TargetProperty} 

,而且據我所知我已經寫了根本的功能沒有差異正確。然而,一個會使用構造函數和另一個設置屬性的想法讓我想到可能是是一個區別,所以我打開反射器並看了一下。

構造函數中有如下代碼:

public Binding(string path) 
{ 
    this._source = UnsetSource; 
    if (path != null) 
    { 
     if (Dispatcher.CurrentDispatcher == null) 
     { 
      throw new InvalidOperationException(); 
     } 
     this._ppath = new PropertyPath(path, new object[0]); 
     this._attachedPropertiesInPath = -1; 
    } 
} 

,path屬性是這樣的:

public PropertyPath Path 
{ 
    get 
    { 
     return this._ppath; 
    } 
    set 
    { 
     base.CheckSealed(); 
     this._ppath = value; 
     this._attachedPropertiesInPath = -1; 
     base.ClearFlag(BindingBase.BindingFlags.PathGeneratedInternally); 
    } 
} 

所以,當你通過屬性設置的路徑PathGeneratedInternally標誌被清除。現在,這個標誌不暴露任何地方公開直接,但它似乎在幾個地方使用:

internal void UsePath(PropertyPath path) 
{ 
    this._ppath = path; 
    base.SetFlag(BindingBase.BindingFlags.PathGeneratedInternally); 
} 

[EditorBrowsable(EditorBrowsableState.Never)] 
public bool ShouldSerializePath() 
{ 
    return ((this._ppath != null) && !base.TestFlag(BindingBase.BindingFlags.PathGeneratedInternally)); 
} 

我敢肯定,這一切都相當微不足道的,但沒有人在那裏知道這是什麼標誌手段以及爲什麼它可能有所不同取決於你如何聲明綁定?

回答

4

關鍵是查看UsePath方法的引用來源。默認情況下,標誌不會被設置,所以清除它基本上是沒有操作的。沒有理由在構造函數中清除它,因爲您知道在這種情況下還沒有設置它(因爲對象仍在構建中)。

UsePath方法只在一個位置被調用,這就是ClrBindingWorker構造函數。如果你看看那裏,你會看到他們自動創建一個「空白」或「空」的路徑,並將其傳遞給UsePath。

我懷疑他們這樣做,因此Path在內部使用時是「有效的」,即使它只是指綁定源(這是沒有給出路徑時的默認行爲)。如果以後在綁定中設置Path屬性,則必須清除指示Path自動生成的標誌。

+0

那麼是否存在功能差異? – 2013-09-15 08:41:35

+0

@UriAbramson - Nope – CodeNaked 2013-09-16 15:56:07

相關問題