I have a list box for which I set ItemContainer to enable the context menu. Here haml for the same.
<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>
I encoded the target method in the ViewModel as shown below.
public void DeleteGroup() {
The ViewModel is set as a DataContext UserControl in which there is a ListBox.
The above code leads to an βnot found target for the method . I donβt know why this does not work. I also tried the following variation
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup" cal:Action.Target="{Binding ElementName=UCRelayDispositionView, Path=DataContext}">
where UCRelayDispositionView is the name of the UserControl.
Why does the above code not work?
Edit: 1 also tried the following
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup" cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView, Path=DataContext}">
and this one
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup" cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView}">
EDIT: 2 I tried using the tag as follows in the ItemContainer, but it does not work either.
<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: Binding Errors
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')