我刚刚在这里回答了一个问题,我说在两者之间没有功能差异
{Binding TargetProperty}
Run Code Online (Sandbox Code Playgroud)
和
{Binding Path=TargetProperty}
Run Code Online (Sandbox Code Playgroud)
而且,据我所知,我所写的内容基本上是正确的.然而,一个人将使用构造函数和另一个设置属性的想法让我觉得可能存在差异,所以我鞭打开放反射器并看了一眼.
构造函数中包含以下代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
path属性是这样的:
public PropertyPath Path
{
get
{
return this._ppath;
}
set
{
base.CheckSealed();
this._ppath = value;
this._attachedPropertiesInPath = -1;
base.ClearFlag(BindingBase.BindingFlags.PathGeneratedInternally);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当您通过属性设置路径时,将清除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));
}
Run Code Online (Sandbox Code Playgroud)
我确定这一切都相当无关紧要,但有没有人知道这个标志意味着什么,为什么它可能会有所不同取决于你如何声明绑定?
关键是看UsePath方法是从哪里引用的。默认情况下不会设置该标志,因此清除它基本上是无操作的。没有理由在构造函数中清除它,因为您知道在这种情况下尚未设置它(因为对象仍在构造中)。
UsePath 方法仅在一个位置调用,即 ClrBindingWorker 构造函数。如果您查看那里,您会看到它们自动创建一个“空白”或“空”路径并将其传递给 UsePath。
我怀疑他们这样做是为了让路径在内部使用时是“有效的”,即使它只是引用绑定源(这是没有给出路径时的默认行为)。如果您稍后在绑定上设置 Path 属性,则必须清除指示 Path 是自动生成的标志。
| 归档时间: |
|
| 查看次数: |
861 次 |
| 最近记录: |