2013-04-24 109 views
1

我想要使用使用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> 

回答

0

這應該工作

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

使用您的列表框ALS的ElementName在CommandParameter結合的名稱,並設置路徑的SelectedItem。

更新:

上面的代碼不會爲列表框和文本菜單的工作,因爲他們屬於不同勢的視覺樹。結果是

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

這似乎是合乎邏輯,但:'System.Windows.Data錯誤:4:參考「的ElementName = lstEmployee」綁定無法找到來源。 BindingExpression:路徑=的SelectedItem;的DataItem = NULL;目標元素是'MenuItem'(Name ='');目標屬性是'CommandParameter'(類型'對象')' – Echilon 2013-04-25 09:53:48

+0

@Echilon我已經更新了我的答案 – Jehof 2013-04-25 10:12:54

+0

真棒,謝謝。 – Echilon 2013-04-25 10:26:21

0

這適用於TreeView或帶有MVVM的ListView。 從文本菜單的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>