WPF中NameScope的查找规则详解

2020-01-05 09:50:19王振洲

但是,实际上 NameScope 的查找却是依赖于逻辑树的 —— 这是 FrameworkElement 的功能:


internal static INameScope FindScope(DependencyObject d, out DependencyObject scopeOwner)
{
 while (d != null)
 {
  INameScope nameScope = NameScope.NameScopeFromObject(d);
  if (nameScope != null)
  {
   scopeOwner = d;
   return nameScope;
  }

  DependencyObject parent = LogicalTreeHelper.GetParent(d);

  d = (parent != null) ? parent : Helper.FindMentor(d.InheritanceContext);
 }

 scopeOwner = null;
 return null;
}

非常明显,FindScope 是期望使用逻辑树来查找名称范围的。

不过值得注意的是,当一个元素没有逻辑父级的时候,会试图使用 Helper.FindMentor 来查找另一个对象。那这是什么方法,又试图寻找什么对象呢?

Mentor 是名词,意为 “导师,指导”。于是我们需要阅读以下 Helper.FindMentor 方法的实现来了解其意图:

提示:以下注释中的 FE 代表 FrameworkElement,而 FCE 代表 FrameworkContentElement。


/// <summary>
///  This method finds the mentor by looking up the InheritanceContext
///  links starting from the given node until it finds an FE/FCE. This
///  mentor will be used to do a FindResource call while evaluating this
///  expression.
/// </summary>
/// <remarks>
///  This method is invoked by the ResourceReferenceExpression
///  and BindingExpression
/// </remarks>
internal static DependencyObject FindMentor(DependencyObject d)
{
 // Find the nearest FE/FCE InheritanceContext
 while (d != null)
 {
  FrameworkElement fe;
  FrameworkContentElement fce;
  Helper.DowncastToFEorFCE(d, out fe, out fce, false);

  if (fe != null)
  {
   return fe;
  }
  else if (fce != null)
  {
   return fce;
  }
  else
  {
   d = d.InheritanceContext;
  }
 }

 return null;
}

具体来说,是不断查找 InheritanceContext,如果找到了 FrameworkElement 或者 FrameworkContentElement,那么就返回这个 FE 或者 FCE;如果到最终也没有找到,则返回 null。