如何在 Windows 8 商店应用程序中访问 Parent 的 DataContext应用程序、商店、如何在、DataContext

2023-09-06 14:12:39 作者:你说过

这是基于 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?

RelativeSourceAncestorTypeMode=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?

(我知道 TemplateBindingElementBinding 但两者都不适用于 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}" />

揭秘Windows 8令苹果生畏的十大传言

在嵌套元素中通过ElementName绑定你想要的属性,即

Bind the property you want via ElementName in nested element, ie

{Binding Tag.SomeProp, ElementName=gridTop}