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

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

问题描述

根据MSDN x:Reference上的 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属性页面MSDN,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:参考和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:Reference相同的技术.搜索算法在VisualTree中向上和/或向下移动以找到所需的元素.因此,始终需要功能性的VisualTree.例如,当在Non-UiElement中使用时,它将不起作用.最终,装订夹住了,做着日常的面包.

{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天全站免登陆