绑定元素名称.是使用可视化树还是逻辑树 [英] Binding ElementName. Does it use Visual Tree or Logical Tree

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

问题描述

有了{Binding ElementName=foo},它会查找可视化树还是逻辑树?

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

WPF 中的逻辑树和可视树 |数据看,数据做

逻辑树什么时候重要?

查找名称时,例如在{Binding ElementName=Foo},搜索走上祖先寻找一个名称范围,就像它所做的一样可继承的属性.

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.

ElementName 绑定Silverlight 通过附加行为

为了实现这一点,WPF 提供了元素名称和相对来源绑定,给你一个强大的定位其他元素的机制在要绑定到的可视化树中

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 Class:

According to MSDN article FrameworkElement Class:

FrameworkElement 扩展了 UIElement并添加了以下功能:

FrameworkElement extends UIElement and adds the following capabilities:

  • 支持数据绑定和动态资源引用:对数据的属性级支持绑定和资源实现通过 DependencyProperty 类和体现在财产制度中,但解析成员值的能力存储为表达式(底层的编程结构数据绑定和动态资源)由框架元素.更多信息,请参见数据绑定概述和资源概览.

参数 #2.

ElementName 指向 x:Name,所以这个名字应该通过某种方式找到.有一个 NameScope 概念.

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

对于大多数情况,FindNameFrameworkElement 上公开的方法和 FrameworkContentElement 更多调用搜索的适当方法对于按名称的元素.名字FrameworkElement 公开的属性和 FrameworkContentElement 更多用于设置的适当属性初始名称作为标记属性.并且公开的 RegisterName 方法在 FrameworkElement 和FrameworkContentElement 是必要的建立一个特定的名称namescope(没有 NameScope可以直接执行此操作的成员;你必须首先设置当前名称范围使用注册名称).

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 中没有找到名为border"的元素,但是将 labelBar 绑定中的 ElementName 更改为buttonFoo"会使背景绿色,正如预期的那样.

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.

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

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