首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >支持MVVM Light的CommandParameter

支持MVVM Light的CommandParameter
EN

Stack Overflow用户
提问于 2013-04-24 15:46:10
回答 1查看 2.8K关注 0票数 1

我正在尝试使用MVVM Light让RelayCommand与CommandParameter一起工作。该命令是在我的视图模型中定义的,我希望将所选的ListBox项作为参数传递。命令已绑定,但参数未绑定。这个是可能的吗?

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2015-07-16 12:05:00

这适用于具有MVVM的TreeView或ListView。来自ContextMenu的PlacementTarget是(在这里)列表视图

代码语言:javascript
复制
<ListView.ContextMenu>
  <ContextMenu>
     <MenuItem x:Name="OpenExplorer"Header="ShowFolder"                  
      Command="{Binding ShowExplorer}"
      CommandParameter ="{Binding Path=PlacementTarget.SelectedItem,
            RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
   </ContextMenu>
</ListView.ContextMenu>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16186322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档