gpt4 book ai didi

wpf - PathGeneratedInternally 标志在 WPF 绑定(bind)中的作用是什么?

转载 作者:行者123 更新时间:2023-12-04 20:23:40 26 4
gpt4 key购买 nike

我刚刚回答了一个问题over here我说过两者之间没有功能上的区别

 {Binding TargetProperty}


 {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;
}
}

路径属性是这样的:
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));
}

我敢肯定这一切都无关紧要,但是有没有人知道这个标志的含义以及为什么它可能会根据您声明绑定(bind)的方式而有所不同?

最佳答案

关键是看从哪里引用了 UsePath 方法。默认情况下,不会设置标志,因此清除它基本上是无操作的。没有理由在构造函数中清除它,因为您知道在那种情况下它没有被设置(因为对象仍在构造中)。

UsePath 方法仅在一个位置调用,即 ClrBindingWorker 构造函数。如果您在那里查看,您会看到它们自动创建“空白”或“空”路径并将其传递给 UsePath。

我怀疑他们这样做是因为路径在内部使用时是“有效的”,即使它只是指绑定(bind)源(这是没有给出路径时的默认行为)。如果稍后在 Binding 上设置 Path 属性,则必须清除指示 Path 是自动生成的标志。

关于wpf - PathGeneratedInternally 标志在 WPF 绑定(bind)中的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3504960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com