2017-11-06 17 views
1

タイトルに記載されている場所でICommandにバインドするのに苦労します。ListViewのグリッドのLabelのContextMenuでICommandにバインドできない

まず、私のDataContextはItemで、私のViewModelではないので、何とかこの問題を回避する必要があります。私はすでにItemClickCommandでこれを行なっていましたが、同じ解決法は機能しません:

第2に、ContextMenuはウィンドウの一部ではなく、ビジュアルまたは論理ツリーの一部ではありません。私はこれを回避するためにどのような木工を実装する必要があるのか​​分かりません。

のViewModel:

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> 

EDIT:MenuItem私は、XAML専用で正しい結合の複数の場所にタグを追加しようとしました が、PlacementTarget ContextMenuです。また、ContextMenuは正しいDataContextを認識しません。これをどうやって回避するのですか?

+1

[WPFの可能な重複:MVVMへのContextMenu結合Command](https://stackoverflow.com/questions/3583507/wpf-binding-a-contextmenu-to-an-mvvm-command) – Sinatr

+0

コマンドパラメータとして「self」にバインドすることで解決できる最初の問題は、親 'DataContext'が必要です。次に、ビジュアルツリーを通過します。 – Sinatr

答えて

1

次いでバインドビューモデルにLabelTag性及び親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のように、ラベルではなくContextMenuを参照します。 – Niksen

+0

* ContextMenu *のPlacementTargetはラベルを参照します。あなたは本当にAncestorTypeをContextMenuに設定しましたか? – mm8

+0

あなたの答えは私の問題を解決します!私がまだ混乱していたのは、Visual Studioが間違っていると私に言ったことでした。しかし、コードを実行すると動作します。どうもありがとう! – Niksen

関連する問題