我正在尝试绑定到我的viewmodel上的命令 . 我在视觉树上使用了一个事件触发器 . 我尝试过尝试绑定它的RelativeSource,FindAncestor和AncestorType的许多变体 . 每次我得到绑定路径表达式错误 .
这是我的xaml文档大纲:
<Window>
<Grid>
<GridView>
<HierarchyChildTemplate>
<DataTemplate>
<TabControl>
<TabItem>
<GridView>
<RowDetailsTemplate>
<TabControl>
<!--trying to bind a event trigger here to a command on the viewModel -->
这是我尝试过的绑定示例:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=SelectionChangedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
如何从xaml中注明的位置绑定此命令?
2 回答
基于您的可视树和您在评论中写的内容(我还没有测试过):
也就是说,我建议在这种情况下使用命名绑定....给最顶部的网格(或原始窗口)命名,例如:
然后,您可以使用
ElementName
绑定:关于你在评论中所说的内容(AncestorType到达了错误的网格),使用
AncestorLevel=2
应该可以帮助你绑定到最顶层的网格 .结束尝试这个,它工作:
这里的关键点是使用DataContext和AncestorLevel = 2来命令命令 .