Wpf 带有MVVM指示灯的CommandParameter
我正试图获得一个RelayCommand,它使用一个CommandParameter,使用MVVM Light。该命令在我的viewmodel中定义,我希望将选定的ListBox项作为参数传递。命令已绑定,但参数未绑定。这可能吗Wpf 带有MVVM指示灯的CommandParameter,wpf,command,mvvm-light,datacontext,commandparameter,Wpf,Command,Mvvm Light,Datacontext,Commandparameter,我正试图获得一个RelayCommand,它使用一个CommandParameter,使用MVVM Light。该命令在我的viewmodel中定义,我希望将选定的ListBox项作为参数传递。命令已绑定,但参数未绑定。这可能吗 <UserControl x:Class="Nuggets.Metro.Views.EmployeeListView" ... DataContext="{Binding EmployeeList,Source={StaticR
<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>
这应该行得通
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Edit item"
Command="{Binding EditEmployeeCommand}"
CommandParameter="{Binding SelectedItem,ElementName=lstEmployee}"/>
<MenuItem Header="Delete item"
Command="{Binding DeleteEmployeeCommand}"
CommandParameter="{Binding SelectedItem,ElementName=lstEmployee}"/>
</ContextMenu>
下面的XAML完成了这项工作。使用ContextMenu的PlacementTarget(即列表框)
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Edit item"
Command="{Binding EditEmployeeCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
<MenuItem Header="Delete item"
Command="{Binding DeleteEmployeeCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
</ContextMenu>
这适用于带有MVVM的TreeView或ListView。 ContextMenu中的PlacementTarget(此处)是Listview
<ListView.ContextMenu>
<ContextMenu>
<MenuItem x:Name="OpenExplorer"Header="ShowFolder"
Command="{Binding ShowExplorer}"
CommandParameter ="{Binding Path=PlacementTarget.SelectedItem,
RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
</ListView.ContextMenu>
这似乎合乎逻辑,但:System.Windows.Data错误:4:找不到引用为“ElementName=lstmemployee”的绑定源。BindingExpression:Path=SelectedItem;DataItem=null;目标元素是“MenuItem”(名称=“”);目标属性为“CommandParameter”(类型为“Object”)
@Echilon这似乎与您使用的代码相同。对我来说这很有效。也许您的代码中还有另一个问题
<ListView.ContextMenu>
<ContextMenu>
<MenuItem x:Name="OpenExplorer"Header="ShowFolder"
Command="{Binding ShowExplorer}"
CommandParameter ="{Binding Path=PlacementTarget.SelectedItem,
RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
</ListView.ContextMenu>