wpf - 绑定到 Window 祖先的元素
问题描述
在这段代码中,我在标签中正确打印了窗口标题
<Window x:Class="Crono4.Views.MainWindow"
...>
<DockPanel>
<Grid ...>
<RadioButton x:Name="buttonProduct" Content="Product"/>
<RadioButton .../>
</Grid>
<Grid>
<Label x:Name="label" Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Title}"/>
</Grid>
</DockPanel>
</Window>
但是如果我想引用按钮的名称,这是行不通的:
<Label x:Name="label" Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=buttonProduct.Content}"/>
为什么?谢谢,
解决方案
因为buttonProduct
不是窗口的属性并且只能绑定到公共属性。
您可以尝试使用 anElementName
绑定到RadioButton
:
<Label x:Name="label" Content="{Binding ElementName=buttonProduct, Path=Content}"/>
推荐阅读
- node.js - NodeJs 多供应商站点和产品访问
- python - 为什么我在 PostListView 中收到“post”的关键错误
- node.js - 如何在 iis 虚拟目录中部署 node.js 应用程序
- javascript - React + Django 中的 URL 路由问题
- qt - 从 C++ 类调用 QML 函数?
- python - 在不平衡数据集上解释 AUC、准确性和 f1 分数
- django - 形式和模型之间的循环依赖
- php - MySQL没有将转义字符转换为原始字符串
- kotlin - 持久性.CordaPersistence。- 清理任务失败:[errorCode=1d5efik, moreInformationAt=https://errors.corda.net/OS/4.4/1d5efik]
- r - r中的随机效应模型