【发布时间】:2010-12-21 18:36:33
【问题描述】:
我有一个ListBox,我添加了一个ContextMenu。我希望将ContextMenu 中的一项绑定到命令,并且我希望传递给该命令的参数成为ListBox 控件的当前选定项。这是我的 xaml:
<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 属性。
帮助?
【问题讨论】:
标签: wpf data-binding listbox commandbinding