wpf - 绑定(bind)元素名称。它使用可视树还是逻辑树

标签 wpf binding elementname visual-tree logical-tree

{Binding ElementName=foo} ,它会查找视觉树还是逻辑树?

Of logical and visual trees in WPF | Data See, Data Do

When does the logical tree matter?

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 binding in Silverlight via Attached Behaviours

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 用于绑定(bind)的逻辑树.

论据#1。

根据 MSDN 文章 FrameworkElement Class :

FrameworkElement extends UIElement and adds the following capabilities:

  • Support for data binding and dynamic resource references: The property-level support for data binding and resources is implemented by the DependencyProperty class and embodied in the property system, but the ability to resolve a member value that is stored as an Expression (the programming construct that underlies both data binding and dynamic resources) is implemented by FrameworkElement. For more information, see Data Binding Overview and Resources Overview.


论点#2。
ElementName指向 x:Name ,所以应该如何找到这个名称。有一个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).



另一方面,Visual class也没有FindName方法,也不执行 INameScope .

最佳答案

我认为这是逻辑树。使用 ControlTemplates 时,您将一个可视化树替换为另一个,但我认为您不能引用 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 绑定(bind)中的 ElementName 更改为“buttonFoo”会使背景变为绿色,如预期的那样。

关于wpf - 绑定(bind)元素名称。它使用可视树还是逻辑树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/705853/

相关文章:

c# - RelativeSource 适用于(嵌套的)子属性,而 ElementName 不适用

data-binding - Xamarin.Forms View 到 View 绑定(bind)而不更改 BindingContext

WPF DataGrid 需要在网格行中垂直滚动条

wpf - "DataContext"命名空间中不存在属性 "http://schemas.microsoft.com/expression/blend/2008"

wpf - 清除 ObservableCollection 时,e.OldItems 中没有项目

c# - Ninject InRequestScope 丢失

java - 使用Java进行XML解析: <element name ="doctype">

wpf - 自定义控件中 ContextMenu 的正确绑定(bind)

c# - WPF DateTimeUpDown 绑定(bind)问题

binding - wp7 SoundEffect 播放绑定(bind)到滚动条