我正在尝试使用MVVM Light让RelayCommand与CommandParameter一起工作。该命令是在我的视图模型中定义的,我希望将所选的ListBox项作为参数传递。命令已绑定,但参数未绑定。这个是可能的吗?
<UserControl x:Class="Nuggets.Metro.Views.EmployeeListView"
...
DataContext="{Binding EmployeeList,Source={StaticResource Locator}}">
<ListBox x:Name="lstEmployee" ItemsSource="{Binding EmployeeItems}" Style="{StaticResource EmployeeList}" Tag="{Binding EmployeeItems}">
<ListBox.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Edit item" Command="{Binding EditEmployeeCommand}" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
<MenuItem Header="Delete item" Command="{Binding DeleteEmployeeCommand}" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
</ContextMenu>
</ListBox.ContextMenu>发布于 2015-07-16 12:05:00
这适用于具有MVVM的TreeView或ListView。来自ContextMenu的PlacementTarget是(在这里)列表视图
<ListView.ContextMenu>
<ContextMenu>
<MenuItem x:Name="OpenExplorer"Header="ShowFolder"
Command="{Binding ShowExplorer}"
CommandParameter ="{Binding Path=PlacementTarget.SelectedItem,
RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
</ListView.ContextMenu>https://stackoverflow.com/questions/16186322
复制相似问题