简体   繁体   English

WP7 xaml中的共享上下文菜单

[英]Share context menu in WP7 xaml

I want to add a contextmenu to items in a listbox. 我想将上下文菜单添加到列表框中的项目。 Typically the answer is to add the contextmenu to the root of the item template. 通常,答案是将上下文菜单添加到项目模板的根目录。 However, I am using a template selector, so there are multiple templates in use, depending on the data of each item in the listbox. 但是,我使用的是模板选择器,因此有多个模板在使用中,具体取决于列表框中每个项目的数据。 This means I need to add the same contextmenu definition to each template, which is not very appealing. 这意味着我需要为每个模板添加相同的上下文菜单定义,这并不是很吸引人。

One solution is to wrap the datatemplate in a ContentControl, which would give me a single place for the context menu definition. 一种解决方案是将数据模板包装在ContentControl中,这将为我提供上下文菜单定义的唯一位置。 However, I believe this would add layout overhead that isn't necessary. 但是,我相信这会增加不必要的布局开销。

Another solution I tried is adding the ContextMenu to the resource dictionary, but I believe this ends up sharing the same object instance across all the uses, and due to the way ContextMenu is implemented, this also does not work. 我尝试过的另一种解决方案是将ContextMenu添加到资源字典中,但是我相信这最终会在所有使用中共享相同的对象实例,并且由于实现ContextMenu的方式,所以这也不起作用。

A third solution is using the Loaded event to call a function which populates the context menu appropriately. 第三种解决方案是使用Loaded事件调用适当填充上下文菜单的函数。 However, this ends up moving a lot of code that ought to be in the XAML into code, and looks quite ugly. 但是,这最终将许多本应在XAML中的代码移动到代码中,并且看起来非常难看。 If there's some way of defining the context menu in xaml, and then just referencing that from code, I would find it appealing, but I don't quite see how to do that. 如果有某种方法可以在xaml中定义上下文菜单,然后仅从代码中引用它,我会觉得它很吸引人,但是我不太清楚该怎么做。

What is the right way to share the same ContextMenu across the templates in a template selector? 在模板选择器中的模板之间共享相同的ContextMenu的正确方法是什么?


This is the ContentControl method, which works, but ends up adding two content controls to each item: 这是ContentControl方法,该方法有效,但最终为每个项目添加了两个内容控件:

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ContentControl>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu Loaded="ContextMenu_Loaded">
                                <toolkit:MenuItem Header="Delete"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <ContentControl ContentTemplate="{StaticResource MyTemplate}" Content="{Binding}"/>
                    </ContentControl>
                </DataTemplate>
            </ListBox.ItemTemplate>

How about adding the ContextMenu to the TemplateSelector? 如何将ContextMenu添加到TemplateSelector?

<ListBox ItemsSource="{Binding}">
    <ListBox.Resources>
        <DataTemplate x:Key="MyTemplate">
            <StackPanel>
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <local:CustomTemplateSelector Content="{Binding}">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu>
                        <toolkit:MenuItem Header="Delete"
                                            Click="MenuItem_Click" />
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <local:CustomTemplateSelector.TemplateOne>
                    <DataTemplate>
                        <ContentControl Content="{Binding}"
                                        ContentTemplate="{StaticResource MyTemplate}"
                                        Foreground="Blue" />
                    </DataTemplate>
                </local:CustomTemplateSelector.TemplateOne>
                <local:CustomTemplateSelector.TemplateTwo>
                    <DataTemplate>
                        <ContentControl Content="{Binding}"
                                        ContentTemplate="{StaticResource MyTemplate}"
                                        Foreground="Red" />
                    </DataTemplate>
                </local:CustomTemplateSelector.TemplateTwo>
                <local:CustomTemplateSelector.TemplateThree>
                    <DataTemplate>
                        <ContentControl Content="{Binding}"
                                        ContentTemplate="{StaticResource MyTemplate}"
                                        Foreground="Yellow" />
                    </DataTemplate>
                </local:CustomTemplateSelector.TemplateThree>
            </local:CustomTemplateSelector>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I ran this and it worked for me - give it a try and let me know if this was the effect you were looking for or not. 我运行了它,它为我工作了-试试看,让我知道这是否是您想要的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM