2013-04-24 2 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> 

사용하여 CommandParameter의 결합에옵니다 루게릭 병 ElementName에 이름을 작업하고 selectedItem가의 경로를 설정해야합니다.

업데이트 : 그들은 시각적 트리를 diffrent 속한 원인

은 위의 코드는,리스트 박스와의 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> 
+0

이것은 논리적 보인다 (여기)이지만,'System.Windows.Data 오류 : 4 : 'ElementName을 = lstEmployee'을 참조하여 바인딩 소스를 찾을 수 없습니다. BindingExpression : Path = SelectedItem; DataItem = null; 대상 요소는 'MenuItem'(Name = ''); 대상 속성은 'CommandParameter'('Object'유형)입니다. – Echilon

+0

@Echilon 전 대답을 업데이트했습니다 – Jehof

+0

감사합니다. – Echilon

0

MVVM을 사용하는 TreeView 또는 ListView에서 작동합니다. 의 ContextMenu에서 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>