我想要使用使用MVVM Light的CommandParameter來使用RelayCommand。該命令在我的viewmodel中定義,並且我想將所選的ListBox項目作爲參數傳遞。該命令是綁定的,但該參數不是。這可能嗎?使用MVVM Light的CommandParameter
<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>
這似乎是合乎邏輯,但:'System.Windows.Data錯誤:4:參考「的ElementName = lstEmployee」綁定無法找到來源。 BindingExpression:路徑=的SelectedItem;的DataItem = NULL;目標元素是'MenuItem'(Name ='');目標屬性是'CommandParameter'(類型'對象')' – Echilon 2013-04-25 09:53:48
@Echilon我已經更新了我的答案 – Jehof 2013-04-25 10:12:54
真棒,謝謝。 – Echilon 2013-04-25 10:26:21