2012-11-27 11 views
6

ItemContainerをスタイリングしてコンテキストメニューを含むリストボックスがあります。同じもののためのxamlはここにあります。"メソッドのターゲットが見つかりません" Caliburn Message.Attach()によってスローされました。

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
    ... 
     <Setter Property="ContextMenu"> 
      <Setter.Value> 
       <ContextMenu> 
        <MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"/> 
       </ContextMenu> 
      </Setter.Value> 
     </Setter> 
    </Style> 

私は以下のように、ViewModelでターゲットメソッドをコーディングしました。

public void DeleteGroup() { //ToDo 
    ... 
} 

ViewModelは、ListBoxがあるUserControlのDataContextとして設定されます。

上記のコードの結果、は「メソッドのターゲットが見つかりません」となります。なぜこれがうまくいかないのか分かりません。私はまた、次のバリエーションを試しました。

<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup" 
      cal:Action.Target="{Binding ElementName=UCRelayDispositionView, Path=DataContext}"> 

ここで、UCRelayDispositionViewはUserControlの名前です。

なぜ上記のコードは機能しませんか?

編集:1 はまた、次の

<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup" 
      cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView, Path=DataContext}"> 

この

<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup" 
      cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView}"> 

EDITみました:私はItemContainerに、次のようにタグを使用しようとしました2 をが、どちらもうまくいかない。

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Tag" Value="{Binding Path=DataContext, ElementName=UCRelayDispositionView}"/> 
     <Setter Property="ContextMenu"> 
      <Setter.Value> 
       <ContextMenu> 
        <MenuItem Header="Remove Group" 
           cal:Message.Attach="DeleteGroup()" 
           cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"/>          
        </ContextMenu> 
      </Setter.Value> 
    </Style> 
</ListBox.ItemContainerStyle> 

EDIT 3:バインディングエラー

System.Windows.Data Error: 40 : BindingExpression path error: 'PlacementTarget' property not found on 'object' ''MenuItem' (Name='')'. BindingExpression:Path=PlacementTarget.Tag; DataItem='MenuItem' (Name=''); target element is 'MenuItem' (Name=''); target property is 'TargetWithoutContext' (type 'Object') 
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=UCRelayDispositionView'. BindingExpression:Path=DataContext; DataItem=null; target element is 'ContextMenu' (Name=''); target property is 'Tag' (type 'Object') 

答えて

10

あなたの問題は、例えば同じビジュアルツリー内に存在しない要素にターゲットをバインドしようとしていることですアイテムが存在するContextMenuがあります。

アクションターゲットを正しく取得するには、ContextMenuPlacementTargetプロパティを使用する必要があります。

したがって、次のXAMLは、動作するはずXAML

WPF Context Menus in Caliburn Micro

用SO上で次の答えをチェックアウト:

<MenuItem Header="Blah" cal:Message.Attach="SomeMethod()" cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"> 

これはContextMenuPlacementTargetを探して、目標を設定する必要がありますアクションの値はPlacementTarget.Tag(これはListBoxItemである必要があります)。

あなたがそうタグ結合があるべき

[OK]をする必要があり、親コンテナ(ListBox)のDataContextであることを(あなたがすでに行っているとして)ListBoxItem.Tagを設定した場合:

例えば
<Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/> 

全体は次のようになります:

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/> 
     <Setter Property="ContextMenu"> 
      <Setter.Value> 
       <ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"> 
        <MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup()" /> 
       </ContextMenu> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ListBox.ItemContainerStyle> 
+0

コードは機能しません。それでも同じエラーが出ます。タグを使用する限り、どこに配置するのですか? ItemContainerStyleにこのコンテキストメニューがあります! – Jatin

+0

私が投稿したSOのリンクを見たら、どこに置くのかの例があります。基本的には 'ContextMenu.PlacementTarget'プロパティを使ってコンテキストメニューを生成し、それを' DataContext'にバインドする項目を取得する必要がありますが、親コンテナの 'DataContext'を' Tag'プロパティにハックする必要があります。これは私のために働いています(メニュー項目をクリックしたときにコードビハインドに入り、デバッグできるように、ビュー内にイベントを作成する価値があります。この方法で、オブジェクトグラフを探索して正しいバインディングパスを見つけることができます) – Charleh

+0

** EDIT 2 **と記された質問を編集しました。このようにタグを設定するためにスタイルを使用できるかどうかはわかりませんが、同じエラーが残っています。 – Jatin

関連する問題