WPF Context Menus in Caliburn Micro

jonnii picture jonnii · Apr 12, 2012 · Viewed 10.2k times · Source

I'm trying to get a context menu within a ListBox ItemTemplate to call a method on the parent view model, passing in the item that was clicked on as a parameter. I have this working for other buttons in the item template, but for the context menu it seems to be failing.

I have the following xaml (abbreviated for clarity):

<ListBox>
    <ListBox.GroupStyle>
        <GroupStyle>
            ...
        </GroupStyle>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ContextMenu>
                    <ContextMenu Name="cm">
                        <MenuItem Header="Open" 
                                  cal:Message.Attach="Open($dataContext)">

                        </MenuItem>
                </Grid.ContextMenu>

                <TextBlock VerticalAlignment="Center" >
                    .. text..
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I have a feeling this has to do with the fact that the visual tree is different, so Caliburn is unable to resolve the method reliably. I'm sure this is a common problem, and I've tried a few of the things I've found online, but nothing seems to be working.

Any ideas??

Answer

Jason Massey picture Jason Massey · Apr 24, 2012

Using the information I found on the Caliburn Micro site I modified your XAML to look like this:

  <Grid Background="White" HorizontalAlignment="Stretch" Height="200" Name="GridLayout">       
    <ListBox x:Name="ListBoxItems">            
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Tag="{Binding DataContext, ElementName=GridLayout}">
                    <Grid.ContextMenu>
                        <ContextMenu Name="cm" cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                            <MenuItem Header="Open" 
                              cal:Message.Attach="Open($dataContext)">
                            </MenuItem>
                        </ContextMenu>
                    </Grid.ContextMenu>

                    <TextBlock VerticalAlignment="Center" >
                .. text..
                    </TextBlock>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

And my view model:

    public List<string> ListBoxItems { get; set; }
    public ShellViewModel()
    {
        ListBoxItems = new List<string> {"One", "Two", "Three"};          
    }

    public void Open(object source)
    {
        MessageBox.Show((string) source);
    }

Open was successfully called with the appropriate strings from the list box.