WinRT(Win 8)商店应用XAML绑定RelativeSourceMode FindAncestor缺失?

Gop*_*ope 14 data-binding winrt-xaml

有人知道XAML绑定中FindAncestor的"新方法"是什么吗?查看RelativeSourceMode(http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.relativesourcemode),只有用于引用self或TemplatedParent的选项.FindAncestor消失了.我想知道为什么以及使用FindAncestor解决的问题的典型解决方案是什么.

最好的问候Gope

Ome*_*viv 14

为什么它会丢失?不知道,但我怀疑除了"微软没有足够的时间及时实现所有功能"之外还有其他解释.

一个简单的解决方法是使用ElementName,如:

Binding={ ElementName=TheNameOfTheAncesor, Path=DataContext.TheViewModelProperyIWantToBindTo }
Run Code Online (Sandbox Code Playgroud)

要向祖先添加x:Name属性,您希望FindAncestor绑定指向:

<TextBlock x:Name="TheNameOfTheAncestor" />
Run Code Online (Sandbox Code Playgroud)

  • 是的,它缺失了...... :) ElementName在简单的场景中工作,但如果在ControlTemplate中则不行,因为每个模板化控件的ElementNames都不同. (2认同)