绑定的ElementName。是否使用可视化树或逻辑树 [英] Binding ElementName. Does it use Visual Tree or Logical Tree

查看:165
本文介绍了绑定的ElementName。是否使用可视化树或逻辑树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{结合的ElementName =美孚} ,它会查找视觉或逻辑树?

Having {Binding ElementName=foo}, will it lookup visual or logical tree?

中在WPF逻辑和视觉树|数据可以看出,数据确实

在做逻辑树怎么了?

当仰视的名称,如在
  {结合的ElementName =美孚},搜索
  走了祖先寻找
  命名范围,再就如同它为
  可继承属性。

When looking up a name, such as in {Binding ElementName=Foo}, the search walks up the ancestry looking for a name scope, again just as it does for inheritable properties.

<一个href=\"http://blog.scottlogic.com/2009/02/22/elementname-binding-in-silverlight-via-attached-behaviours.html\"相对=nofollow>的ElementName通过在Silverlight结合附行为研究

为了使这一点,WPF提供
  的ElementName和的RelativeSource
  绑定,给你一个强大的
  机构定位等元素
  你的视觉树中绑定到

In order to enable this, WPF provides ElementName and RelativeSource bindings, giving you a powerful mechanism for locating other elements within your visual tree to bind to

编辑:

它看起来像用于通过的ElementName 绑定逻辑树。

It looks like the Logical Tree used for binding by ElementName.

参数#1。

根据MSDN文章 FrameworkElement的类

FrameworkElement的延伸的UIElement
  并增加了以下功能:

FrameworkElement extends UIElement and adds the following capabilities:


      
  • 数据绑定支持和
      动态资源参考文献:
      数据属性级别的支持
      结合和资源实现
      由的DependencyProperty类
      体现在产权制度,但
      解决成员价值的能力
      存储作为Ex pression(中
      编程结构underlies
      这两个数据绑定和动态
      资源)由实施
      FrameworkElement的。欲了解更多
      信息,请参见数据绑定概述
      和资源概述。

  •   

参数#2。

的ElementName X:名称,所以这个名字应该可以找到一些如何。有一个名称范围概念。

ElementName points to x:Name, so this name should be found some how. There is a NameScope concept.

有关大多数情况下,该FindName
  方法暴露在FrameworkElement的
  和FrameworkContentElement上更
  适当的方法来调用搜索
  按名称的元素。名字
  性能由FrameworkElement的暴露
  和FrameworkContentElement上更
  相应的属性用来设置
  最初的名称标记属性。
  而RegisterName方法曝光
  在FrameworkElement的和
  FrameworkContentElement上是必要的
  建立一个名称的特定
  名称范围(不存在名称范围
  构件,可直接执行此;您
  必须先设置当前名称范围
  使用RegisterName)。

For most scenarios, the FindName methods exposed on FrameworkElement and FrameworkContentElement are more appropriate methods to call to search for elements by name. The Name properties exposed by FrameworkElement and FrameworkContentElement are more appropriate properties to use to set the initial name as markup attributes. And the RegisterName methods exposed on FrameworkElement and FrameworkContentElement is necessary to establish a name into a specific namescope (there is no NameScope member that can do this directly; you must set the current namescope first to use RegisterName).

在另一方面,视觉类也没有 FindName 的方法,也没有实施的 INameScope

On the other hand, Visual class neither have FindName method, nor implement INameScope.

推荐答案

我认为这是合乎逻辑的树。当使用CONTROLTEMPLATES,你与另一个替换一个可视树,但我不认为你可以参考的ControlTemplate中定义的名称。

I think it's logical tree. When using ControlTemplates, you're replacing one visual tree with another, but I don't think you can reference the names defined inside of the ControlTemplate.

例如:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
    	<Grid.Resources>
    		<ControlTemplate x:Key="Foo" TargetType="Button">
    			<Border x:Name="border" Background="Red">
    	  			<Label Content="{TemplateBinding Content}"></Label>
    			</Border>
    		</ControlTemplate>
      	</Grid.Resources>
      	<Grid.ColumnDefinitions>
    		<ColumnDefinition></ColumnDefinition>
      		<ColumnDefinition></ColumnDefinition>
      	</Grid.ColumnDefinitions>
      	<Button x:Name="buttonFoo" Background="Green" HorizontalAlignment="Center" VerticalAlignment="Center" Template="{DynamicResource Foo}">Foo</Button>
      	<Label x:Name="labelBar" Grid.Column="1"  HorizontalAlignment="Center" VerticalAlignment="Center" Background="{Binding ElementName=border, Path=Background}">Bar</Label>
    </Grid>
</Page>

不找到指定的元素在ControlTemplate中边界,但在labelBar的结合buttonFoo转变的ElementName使得背景绿色,符合市场预期。

Doesn't find the element named "border" in the ControlTemplate, but changing ElementName in labelBar's binding to "buttonFoo" makes the Background Green, as expected.

这篇关于绑定的ElementName。是否使用可视化树或逻辑树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆