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')
コードは機能しません。それでも同じエラーが出ます。タグを使用する限り、どこに配置するのですか? ItemContainerStyleにこのコンテキストメニューがあります! – Jatin
私が投稿したSOのリンクを見たら、どこに置くのかの例があります。基本的には 'ContextMenu.PlacementTarget'プロパティを使ってコンテキストメニューを生成し、それを' DataContext'にバインドする項目を取得する必要がありますが、親コンテナの 'DataContext'を' Tag'プロパティにハックする必要があります。これは私のために働いています(メニュー項目をクリックしたときにコードビハインドに入り、デバッグできるように、ビュー内にイベントを作成する価値があります。この方法で、オブジェクトグラフを探索して正しいバインディングパスを見つけることができます) – Charleh
** EDIT 2 **と記された質問を編集しました。このようにタグを設定するためにスタイルを使用できるかどうかはわかりませんが、同じエラーが残っています。 – Jatin