【问题标题】:What does the PathGeneratedInternally flag do in a WPF binding?PathGeneratedInternally 标志在 WPF 绑定中的作用是什么?
【发布时间】:2010-08-17 17:00:05
【问题描述】:

我刚刚回答了一个问题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));
}

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

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

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

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

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

    【讨论】:

    • 那么有功能上的区别吗?
    猜你喜欢
    • 2019-07-27
    • 2022-01-11
    • 1970-01-01
    • 2022-11-20
    • 2011-01-16
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多