WPF 绑定RelativeSource 问题
我正在使用 FindAncestor 和 AncestorLevel=3 到达应该具有 viewModel 中继命令的顶级标签,但它不起作用。如果我做错了,有什么建议或者调试这种情况的方法吗?
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}" Cursor="Hand"
Foreground="Blue" TextDecorations="Underline">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<cmd:EventToCommand Command="{Binding NameClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=3}}"
MustToggleIsEnabled="True" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I'm using FindAncestor and AncestorLevel=3 to reach to the top level tag which should have the viewModel relay command, but it doesnt work. Any suggestions if I am doing it wrong or a way to debug this scenario?
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}" Cursor="Hand"
Foreground="Blue" TextDecorations="Underline">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<cmd:EventToCommand Command="{Binding NameClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=3}}"
MustToggleIsEnabled="True" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在将命令绑定到网格上名为 NameClickCommand 的属性。 Grid 没有此属性,因此
如果 NameClickCommand 位于 Grid 的 DataContext 中,请尝试将其更改为
It seems like you're binding the Command to a property called NameClickCommand on a Grid. Grid doesn't have this property, so try to change it to
if the NameClickCommand is in the DataContext of the Grid
您正在寻找层次结构中的第三个
Grid
——这是您想要的吗?请注意,
Grid
不包括DataGrid
。You're looking for the 3rd
Grid
up the hierarchy -- is that what you want?Note that
Grid
does not includeDataGrid
.