Caliburn Micro中的WPF上下文菜单 [英] WPF Context Menus in Caliburn Micro

查看:100
本文介绍了Caliburn Micro中的WPF上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ListBox ItemTemplate中获得一个上下文菜单,以调用父视图模型上的方法,并将单击的项目作为参数传入。我可以将其用于项目模板中的其他按钮,但是对于上下文菜单来说似乎失败了。

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.

我有以下xaml(为简明起见,简称:):

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>

我觉得这与视觉树不同的事实有关,因此Caliburn是无法可靠地解决该方法。我确定这是一个普遍的问题,我已经尝试了一些我在网上找到的东西,但是似乎没有任何效果。

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.

任何想法

推荐答案

使用在 Caliburn Micro网站我修改了您的XAML,使其看起来像这样:

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>

我的视图模型:

    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。​​

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

这篇关于Caliburn Micro中的WPF上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆