代码之家  ›  专栏  ›  技术社区  ›  Martin Harris

PathGeneratedInternally标志在WPF绑定中做什么?

  •  5
  • Martin Harris  · 技术社区  · 15 年前

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

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

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

    1 回复  |  直到 8 年前
        1
  •  4
  •   CodeNaked    14 年前

    关键是查看UsePath方法的引用位置。默认情况下不会设置标志,因此清除它基本上是不允许的。没有理由在构造函数中清除它,因为您知道在这种情况下没有设置它(因为对象仍在构造中)。

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

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

    推荐文章