x:Reference 和 ElementName 有什么区别? [英] What is the difference between x:Reference and ElementName?

查看:31
本文介绍了x:Reference 和 ElementName 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MSDN 上的 x:Reference Markup Extension 页面,x:参考

According to the x:Reference Markup Extension page on MSDN, x:Reference

引用在 XAML 标记中其他地方声明的实例.引用指的是元素的 x:Name.

References an instance that is declared elsewhere in XAML markup. The reference refers to an element's x:Name.

根据 Binding.ElementName 属性 页,ElementName

感兴趣元素的 Name 属性或 x:Name 指令的值.

The value of the Name property or x:Name Directive of the element of interest.

回顾第一页的备注部分:

Looking back at the remarks section on the first page:

x:Reference 和 WPF

在 WPF 和 XAML 2006 中,元素引用由 ElementName 绑定的框架级功能处理.对于大多数 WPF 应用程序和方案,仍应使用 ElementName 绑定.本一般指南的例外情况可能包括数据上下文或其他范围考虑因素导致数据绑定不切实际以及不涉及标记编译的情况.

In WPF and XAML 2006, element references are addressed by the framework-level feature of ElementName binding. For most WPF applications and scenarios, ElementName binding should still be used. Exceptions to this general guidance might include cases where there are data context or other scoping considerations that make data binding impractical and where markup compilation is not involved.

为了完整起见,这里是 ElementName 页面上备注部分的一部分:

For completeness, here is part of the remarks section on the ElementName page:

当您想绑定到应用程序中另一个元素的属性时,此属性很有用.例如,如果您想使用 Slider 来控制应用程序中另一个控件的高度,或者您想将控件的 Content 绑定到 ListBox 控件的 SelectedValue 属性.

This property is useful when you want to bind to the property of another element in your application. For example, if you want to use a Slider to control the height of another control in your application, or if you want to bind the Content of your control to the SelectedValue property of your ListBox control.

现在,虽然我完全了​​解何时以及如何使用 ElementName 属性,但我并不完全理解它与 x:Reference 标记之间的区别延期.任何人都可以解释这一点,特别是扩展 x:Reference 备注部分中显示的最后一句话吗?:

Now, while I am fully aware of when and how to use the ElementName property, I don't fully understand the difference between it and the x:Reference markup extension. Can anybody please explain this and in particular, expand on the last sentence shown from the x:Reference remarks section?:

此一般指南的例外情况可能包括数据上下文或其他范围考虑因素使数据绑定不切实际以及不涉及标记编译的情况.

Exceptions to this general guidance might include cases where there are data context or other scoping considerations that make data binding impractical and where markup compilation is not involved.

推荐答案

基本上就像你说的,这两个做的差不多.然而,内部存在细微差别.

Basically like you said those two do almost the same. However there are small differences under the hood.

{x:Reference ...} -> 只返回一个对象的引用,它不会像绑定那样在两个属性之间创建桥梁".在这一切背后,正在使用服务在特定范围内搜索给定名称,通常是窗口本身.

{x:Reference ...} -> returns just a reference of an object it doesn't create that "bridge" between two properties like binding would do. Behind all that a service is being used that searches for the given name in a specific scope which is usually the window itself.

{Binding ElementName="..." } -> 首先,它创建绑定对象,然后搜索对象名称,但不使用与 x 相同的技术:参考.搜索算法在 VisualTree 中向上和/或向下移动以查找所需元素.因此,始终需要一个功能性的 VisualTree.例如,在非 UiElement 中使用时,它将不起作用.最后,Binding 留下来并做它的日常面包.

{Binding ElementName="..." } -> first of all it creates that binding object then it searches for the object name but not by using the same technique under the hood as x:Reference. The search algorithm moves up and/or down in VisualTree to find the desired element. Therefore a functional VisualTree is always needed. As example when used inside a Non-UiElement, it won't work. In the end the Binding stays and does its daily bread.

这行不通:

<StackPanel>
 <Button x:name="bttn1" Visibility="Hidden">Click me</Button>
 <DataGrid>
  <DataGrid.Columns>
    <DataGridTextColumn Visibility="{Binding ElementName=bttn1, Path=DataContext.Visibility}"/>
 ....

这有效:

<StackPanel>
 <Button x:name="bttn1" Visibility="Hidden">Click me</Button>
 <DataGrid>
  <DataGrid.Columns>
    <DataGridTextColumn Visibility="{Binding Source={x:Reference bttn1}, Path=DataContext.Visibility}"/>
 ....

有点像:)

这篇关于x:Reference 和 ElementName 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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