2017-11-06 13 views
1

제목에 설명 된 장소에서 ICommand에 바인딩하는 데 어려움이 있습니다.ListView의 Grid의 Label에있는 ContextMenu의 ICommand에 바인딩이 작동하지 않습니다.

첫째, 내 DataContext는 Item이고 내 ViewModel이 아니므로 어떻게 든이 문제를 해결해야합니다. 이미 ItemClickCommand에서이 작업을 수행했지만 다음과 같은 이유로 동일한 솔루션이 작동하지 않습니다.

두 번째로 ContextMenu는 시각적 또는 논리적 트리의 일부가 아닌 창의 일부가 아닙니다. 이 문제를 해결하기 위해 구현해야하는 woodoo는 무엇인지 모르겠습니다.

뷰 모델 :

public ICommand CopyTextCommand { get; private set; } 
public Constructor() 
{ 
    CopyTextCommand = new RelayCommand(InsertToClopboard); 
    Initialize(); 
} 
private void InsertToClopboard(object parameter) 
{ 
    // Want to get in here. 
} 

보기 :

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <ListView Grid.Row="0" x:Name="MainListView" ItemsSource="{Binding Items}" Background="Transparent" BorderBrush="Transparent" Margin="0" HorizontalContentAlignment="Stretch"> 

      <ListView.ItemTemplate> 

       <DataTemplate> 
        <Grid Margin="0" Visibility="{Binding GuiVisibility, Converter={StaticResource BoolToVisibility}}"> 
         <Grid.InputBindings> 
          <MouseBinding Gesture="LeftClick" 
             Command="{Binding Path=DataContext.ItemClickCommand, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" 
             CommandParameter="{Binding}"/> 
         </Grid.InputBindings> 

         <Label ... 
          Content="{Binding PNR}" > 
          <Label.ContextMenu> 
           <ContextMenu> 
            <MenuItem 
             Name="MenuItemPnr" 
             Header="Copy" 
             Command="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.DataContext}" <--This does not work--> 
             CommandParameter="test" /> 
           </ContextMenu> 
          </Label.ContextMenu> 
         </Label> 

        </Grid> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </Grid> 

편집 : MenuItem 내가 XAML 욕실에서 올바른 바인딩 여러 위치에 태그를 추가하는 것을 시도했다 하지만 PlacementTarget ContextMenu입니다. 그리고 ContextMenu는 올바른 DataContext를 알지 못합니다. 이 문제를 어떻게 해결할 수 있습니까?

+1

[WPF가의 가능한 중복 : MVVM에의 ContextMenu 바인딩 명령] (https://stackoverflow.com/questions/3583507/wpf-binding-a-contextmenu-to-an-mvvm-command) – Sinatr

+0

명령 매개 변수로 'self'에 바인딩하여 해결할 수있는 첫 번째 문제는 부모 인'DataContext'가 필요하다면, 시각적 트리를 거쳐야합니다. – Sinatr

답변

1

는 바인딩 다음 뷰 모델에 Label과의 Tag 속성은 부모 ContextMenuPlacementTargetMenuItemCommand 속성을 바인딩 :

<Label Content="{Binding PNR}" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListView}}"> 
    <Label.ContextMenu> 
     <ContextMenu> 
      <MenuItem 
       Name="MenuItemPnr" 
       Header="Copy" 
       Command="{Binding Path=PlacementTarget.Tag.CopyTextCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
       CommandParameter="test" /> 
     </ContextMenu> 
    </Label.ContextMenu> 
</Label> 
+0

내 명령은 태그를 찾을 수 없습니다. PlacementTarget은 Label이 아닌 ContextMenu를 참조합니다. – Niksen

+0

* ContextMenu *의 PlacementTarget은 레이블을 참조합니다. AncestorType을 ContextMenu로 설정 했습니까? – mm8

+0

당신의 대답이 내 문제를 해결합니다! 여전히 혼란 스러웠던 점은 Visual Studio에서 잘못되었다고 말한 것입니다. 그러나 코드를 실행하면 작동합니다. 고마워요! – Niksen