如何在 Windows 8 商店应用程序中访问 Parent 的 DataContext [英] How to access Parent's DataContext in Windows 8 store apps
问题描述
这是基于 XAML 的应用程序 (WPF/Silverlight/WinRT) 中的常见情况.
This is a common situation in XAML based apps (WPF/Silverlight/WinRT).
WPF 相关链接 -> WPF 数据绑定:如何我是否可以访问父母"?数据上下文?
WPF related link -> WPF Databinding: How do I access the "parent" data context?
RelativeSource
和 AncestorType
和 Mode=FindAncestor
通常在 WPF 中起到救援作用.
RelativeSource
with AncestorType
, and Mode=FindAncestor
usually comes to rescue in WPF.
WinRT API 中缺少这两者.如何访问父级(可能不是直接的),DataContext
?
Both of these are missing in WinRT API. How to access the Parent's (may not be immediate one), DataContext
?
(我知道 TemplateBinding
和 ElementBinding
但两者都不适用于 DataTemplate).
(I am aware of TemplateBinding
, and ElementBinding
but both are not suitable mostly in DataTemplate).
推荐答案
我也遇到了同样的问题.想必这很常见??
I just had the same problem. Presumably this is common??
这是一个可行的粗略解决方案:
Here is a crude solution that works:
将顶级元素的 Tag 属性绑定到 DataContext
Bind the Tag property of a top level element to the DataContext
<Grid Name="gridTop" Tag="{Binding}" />
在嵌套元素中通过ElementName绑定你想要的属性,即
Bind the property you want via ElementName in nested element, ie
{Binding Tag.SomeProp, ElementName=gridTop}
这篇关于如何在 Windows 8 商店应用程序中访问 Parent 的 DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!