我有一个ListBox
我已经添加ContextMenu
到的。我希望将其中的一项ContextMenu
绑定到命令,并且我希望传递给该命令的参数成为ListBox
控件的当前选定项。这是我的xml:
<ListBox x:Name="selectedVascularBeds"
ItemsSource="{Binding Path=UserSelectedVascularBeds}"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}"
DisplayMemberPath="VascularBedName">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove" Command="{Binding Path=RemoveSelectedVascularBedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type ListBox}},
Path=SelectedItem}"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
这ListBox
是绑定到视图模型对象的用户控件的一部分。我在底层对象上的命令方法被调用,但传入的参数始终为空。
我已经测试了将绑定更改CommandParameter
为简单地{Binding}
导致用户控件的数据上下文被传递到我的方法中 - 所以我知道该命令正在工作并正确传递参数。我似乎无法获得正确的绑定来访问ListBox
'SelectedItem
属性。
帮助?