2013-04-24 4 views
1

Я пытаюсь получить RelayCommand, работающий с CommandParameter, работающим с использованием MVVM Light. Команда определена в моей модели viewmodel, и я хочу передать выбранный элемент ListBox в качестве параметра. Команда привязана, но параметр отсутствует. Это возможно?CommandParameter с MVVM Light

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

Используйте имя вашего ListBox ALS ELEMENTNAME в связывании CommandParameter и указываем путь к SelectedItem.

Update:

Приведенный выше код не работает для ListBox и ContextMenu, потому что они принадлежат к diffrent зрительные деревьев. Результат

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 выполняет эту работу. Использование PlacementTarget (то есть ListBox) 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> 
+0

Это кажется логичным, но: 'System.Windows.Data Ошибка: 4: Не удается найти источник для связывания со ссылкой 'ElementName = lstEmployee. BindingExpression: Path = SelectedItem; DataItem = NULL; целевым элементом является «MenuItem» (Name = ''); target свойство «CommandParameter» (тип «Object») ' – Echilon

+0

@ Echilon обновил мой ответ – Jehof

+0

Удивительно, спасибо. – Echilon

0

Это работает для TreeView или ListView с MVVM. PlacementTarget от ContextMenu есть (доступен здесь) 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>