wpf x:Reference 和 ElementName 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19244111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:58:09  来源:igfitidea点击:

What is the difference between x:Reference and ElementName?

wpfxamlmarkup

提问by Sheridan

According to the x:Reference Markup Extensionpage on MSDN, x:Reference

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

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

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

According to the Binding.ElementName Propertypage on MSDN, ElementName

根据MSDN 上的Binding.ElementName 属性页,ElementName

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

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

Looking back at the remarks section on the first page:

回顾第一页的备注部分:

x:Reference and WPF

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.

x:参考和WPF

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

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

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

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.

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

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

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

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.

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

回答by dev hedgehog

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

基本上就像你说的,这两个做的几乎一样。然而,引擎盖下存在细微差别。

{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.

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

{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.

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

This won't work:

这行不通:

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

This works:

这有效:

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

Sort of like that :)

有点像这样:)

回答by Michael Brown

ElementName is platform specific. I.e. it may or may not be present based on which platform you're using. x:Reference elevates that concept to a XAML native feature. Thus any platform that supports XAML supports x:Reference.

ElementName 是特定于平台的。即它可能存在也可能不存在,具体取决于您使用的平台。x:Reference 将该概念提升为 XAML 本机功能。因此,任何支持 XAML 的平台都支持 x:Reference。