- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据x:Reference Markup Extension MSDN 页面,x:Reference
References an instance that is declared elsewhere in XAML markup. The reference refers to an element's x:Name.
根据Binding.ElementName Property MSDN 页面,ElementName
The value of the Name property or x:Name Directive of the element of interest.
回顾第一页的备注部分:
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.
为了完整起见,以下是 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.
现在,虽然我完全了解何时以及如何使用 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.
最佳答案
基本上就像你说的那样,两者的作用几乎相同。然而,在幕后却存在着细微的差异。
{x:Reference ...}
-> 仅返回对象的引用,它不会像绑定(bind)那样在两个属性之间创建“桥梁”。在这一切的背后,正在使用一个服务来在特定范围(通常是窗口本身)中搜索给定名称。
{Binding ElementName="..."}
-> 首先,它创建该绑定(bind)对象,然后搜索对象名称,但不使用与 x 相同的技术:引用。搜索算法在 VisualTree 中向上和/或向下移动以找到所需的元素。因此,始终需要一个功能性的 VisualTree。例如,当在非 UiElement 内部使用时,它将不起作用。最后,Binding 留下来并做它的日常工作。
这行不通:
<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}"/>
....
有点像这样:)
关于wpf - x :Reference and ElementName? 和有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19244111/
我的项目中有这个 xaml: // ... 我在 MyControl 类中定义了 IsEditing 依赖属性。 namespace MyNamespace { public pa
我有两个继承自 BaseClass 的子类。 我正在尝试对 BaseClass 类型 的对象使用反射来获取 XmlRootAttribute.ElementName 值。那么,如果有人传入一个 Bas
这是我的网址的一部分 Новый Афон Пицунда Гудаута Гагра Сухум 用户输入城市名称,例如:“Пицунда”,我
我创建了一个显示文件列表的 JTree。这些文件的长度不同。我想在 JTree 中以相同的长度显示它们,例如 20 个字符。如果名称大于 20 个字符,它应该出现在工具提示中。 如何实现?下面是我的
以下哪些 TextBlocks 的绑定(bind)会消耗更多性能: 我确信当 TextBlocks 处于具有许多 sibling 和祖先的高嵌套级别时,我的问
我正在对 Web 应用程序进行一些维护编码,我收到一个 javascript 错误,格式为:“[elementname] 没有属性” 部分代码是通过 AJAX 调用即时生成的,该调用更改了部分页面的
通过名称进行绑定(bind)自引用与通过 RelativeSource 进行自引用之间有什么区别? 例如: 相对 在我的项目中,它们的行为似乎相同,但我直接在窗口中使用它们。这意味着我立即更喜
我有以下 XAML: ... ...
根据x:Reference Markup Extension MSDN 页面,x:Reference References an instance that is declared elsewhere
我已将 MenuFlyout 添加到 ItemsControl.ItemTemplate 中的按钮。我也能够将当前项目绑定(bind)为 CommandParameter。现在我想将 Command
我正在使用 xml,并使用 xsd 来验证我的 xml 文件。我的 xsd 和 xml 文件太长,我可以给你其中的一部分。 XSD 文件: ...
我有一个 Datagrid,它有一个 DataGridTemplateColumn,其中包含一个 ListView,其 Itemsource 绑定(bind)到一个字符串数组。 在该 ListView
我正在尝试将类对象序列化为如下所示的 xml: 0,000 0,000 1,000 1,000 1,000 0,000 0,000 1,000 重要的是没有直
这是 XAML: 绑定(bind)返回错误:System.Windows.Data 错误:2:找不到目标元素的管理 Fram
我有两个 XSD 文件,我想要两个不同的 xsd 文件的元素具有相同的名称但具有不同的属性类型。 假设下面是xml1.xsd te
我有一个datagrid,在datagridtemplatecolumn中,我有一个togglebutton,在检查时,它会在elementName binding的帮助下打开弹出窗口。。
我想创建一个在后台执行操作并可通过托盘图标进行控制的应用程序。这个托盘图标有一个带有复选框的上下文菜单,可以将其设置为已启用,然后后台任务就会启动。 我正在使用 WPF 和 Hardcodet WPF
我正在尝试绑定(bind)一个依赖于同一 DataTemplate 中控件的属性。 举例说明:
我有一个应用程序,它让我可以选择通过 RessourceDictionary 更改应用程序主题,这意味着我只能使用纯 XAML。 现在,我想做的是隐藏一个 DockPanel,它没有 ElementN
使用传统的 {Binding} 语法时,您可以指定元素名称以指向页面上的特定控件,并能够访问其属性。例如,如果页面名为 page你可以这样做: {Binding ElementName=Page, P
我是一名优秀的程序员,十分优秀!