从XAML传递命令参数 [英] Pass command parameter from the xaml
本文介绍了从XAML传递命令参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试做这样的事情:
< DataGrid的名称=myGrid的ItemSource ={绑定路径= MyCollection的}>
< DataGrid.ContextMenu>
<&文本菜单GT;
<菜单项
命令={结合RemoveRow}
CommandParameter ={绑定的ElementName = myGrid,路径=的SelectedItem}>
< /文本菜单>
< / DataGridContextMenu>
< / DataGrid的>
但我得到空总是(我也试过和的SelectedIndex的SelectedValue)
如果我通过以下参数来执行code,它的工作原理:
<菜单项命令={结合RemoveRow}CommandParameter =1>
解决方案
尝试这样的事情在你的CommandParameter,
< DataGrid.ContextMenu>
<&文本菜单GT;
<菜单项标题=MyHeader
命令={结合MyCommand}
CommandParameter ={绑定的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型的ContextMenu}},路径= PlacementTarget.SelectedItem}/>
< /DataGrid.ContextMenu>
我已经测试了它,它应该工作。
I try to do something like this:
<DataGrid Name="myGrid" ItemSource="{Binding Path=MyCollection}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem
Command="{Binding RemoveRow}"
CommandParameter="{Binding ElementName=myGrid, Path=SelectedItem}">
</ContextMenu>
</DataGridContextMenu>
</DataGrid>
but I got null always (I tried also SelectedIndex and SelectedValue)
if I pass the following parameter to the execution code, it works:
<MenuItem Command="{Binding RemoveRow}" CommandParameter="1">
解决方案
Try something like this in your CommandParameter,
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="MyHeader"
Command="{Binding MyCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}" />
</DataGrid.ContextMenu>
I already tested it and it should work.
这篇关于从XAML传递命令参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文