带MVVM Light的CommandParameter(CommandParameter with MVVM Light)

我正在尝试使用使用MVVM Light的CommandParameter来使用RelayCommand。 该命令在我的viewmodel中定义,我想传递选定的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>

I'm trying to get a RelayCommand working with a CommandParameter working using MVVM Light. The command is defined in my viewmodel, and I want to pass the selected ListBox item as the parameter. The command is bound, but the parameter is not. Is this possible?

<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>

在CommandParameter的绑定中使用ListBox als ElementName的名称,并将路径设置为SelectedItem。

更新:

上面的代码不适用于ListBox和ContextMenu,因为它们属于不同的可视树。 结果是

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=lstEmployee'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'CommandParameter' (type 'Object')

以下XAML完成了这项工作。 使用ContextMenu的PlacementTarget(即ListBox)。

<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>

This should work

<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>

Use the Name of your ListBox als ElementName in the binding of the CommandParameter and set the path to SelectedItem.

Update:

The above code does not work for ListBox and ContextMenu, cause they belong to diffrent visual trees. The result is

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=lstEmployee'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'CommandParameter' (type 'Object')

The following XAML does the job. Using the PlacementTarget (that is the ListBox) of the ContextMenu.

<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>

更多推荐