2017-10-10 7 views
1

私はwpfとmvvmを学習しており、自分自身で練習するためのサウンドボードを作成することにしました。 これで、指定されたディレクトリでプログラムが検出するすべてのファイルに対して、そのファイルの名前のボタンが作成され、それをクリックして再生することができるデータテンプレートを作成しました。ここまでは順調ですね。 しかし、リストからファイルを削除するときに右クリックして削除を選択できるようにContextMenuを作成しようとしましたが、通常のボタンのコマンド構造がまったく同じでもこのコマンドは機能しません。C#wpfコンテキストメニューでデータバインディングコマンドが機能しない

私は本当にRelativeSourceの全体のこととかなり混乱していて、私の普通の '再生'コマンドがボタンで働いていて、とても満足していました。

誰かが正しい方向に私を指すことができればそれはすばらしいでしょう。私は本当に私の特定の問題についての説明を使用することができました。なぜなら、いつもより一般的な例を何とか助けてくれるようです。私はすべての関連する質問を読むことを試みたが、そこからそれを理解するようには思われない。

私のItemsControl:

<ItemsControl x:Name="MySounds" ItemsSource="{Binding Sounds}"> 

ItemTemplateに:

<ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <Button Style="{StaticResource mainButton}" 
           Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}" 
           CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" 
           Tag="{Binding Path=Name}"> 
          <TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" /> 
          <Button.ContextMenu> 
           <ContextMenu> 
            <MenuItem Header="{Binding Path=Name}" 
               Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.RemoveSound}" 
               CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"> 
             <MenuItem.Icon> 
              <Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/> 
             </MenuItem.Icon> 
            </MenuItem> 
           </ContextMenu> 
          </Button.ContextMenu> 
         </Button> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 

私は自分のviewmodelにジェネリックRelayCommandを持っており、すべての作品という、問題が実際には結合しています。あなたはItemsControlButtonTagプロパティをバインドする場合は、あなたがのPlacementTargetプロパティを使用して、コマンドにバインドでき

Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.RemoveSound}" 
+0

「RelativeSource」ではなく「ElementName」を使用できますか? ItemsControl(x:Name = "myIC"など)に名前を設定し、バインディングを更新します。 – Atlasmaybe

+0

私はそれを試みましたが、私が何を試してもうまくいかないようです。私のMenuItemヘッダーでは、 '{binding path = Name} 'とボタンの値が同じですが、同じコマンドが機能していないようです。 –

答えて

0

+0

そのトリックをしたようです。 –

+0

なぜこの作品を作ったのでしょうか?タグはRelativeSource ItemsControlに設定され、PlacementTargetはTagに設定され、DataContextに設定されるためですか? RelativeSourceの私の理解が非常に裸である前に私が述べたように。 –

+0

ContextMenuはそれ自身のビジュアルツリーに存在するので、祖先はありません。ボタンは持っている。 – mm8

0

あなたはこれによりあなたのMenuItemであなたのコマンド文字列を置換しようとすることができますContextMenu

<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
      <Button Style="{StaticResource mainButton}" 
           Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}" 
           CommandParameter="{Binding Path=Name}" 
           Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}"> 
       <TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" /> 
       <Button.ContextMenu> 
        <ContextMenu> 
         <MenuItem Header="{Binding Path=Name}" 
            Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.RemoveSound}" 
            CommandParameter="{Binding Path=Name}"> 
          <MenuItem.Icon> 
           <Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/> 
          </MenuItem.Icon> 
         </MenuItem> 
        </ContextMenu> 
       </Button.ContextMenu> 
      </Button> 
     </StackPanel> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
関連する問題