简体   繁体   English

我可以从WP7中的ContextMenu中删除MenuItem吗?

[英]Can I Remove a MenuItem from ContextMenu in WP7

I'm looking for a method - if at all possible - of removing a MenuItem control from a ContextMenu in Windows Phone 7. 我正在寻找一种方法 - 如果可能的话 - 从Windows Phone 7中的ContextMenu中删除MenuItem控件。

Here's a simplified version of my XAML: 这是我的XAML的简化版本:

<ListBox>
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            [ -- Content -- ]
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="view attributes" Tag="ATTRIBUTES" Click="ViewSelectedResultAttributes" />
                    <toolkit:MenuItem Header="view groups" Tag="GROUPS" Click="ViewSelectedResultGroups" />
                    <toolkit:MenuItem Header="view webpage" Tag="ONLINE" Click="ViewWebPage" />
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Now, not all of the items in the bound collection have a website associated with them, and for those items I wish to remove the MenuItem with the tag ONLINE ( ie the last MenuItem). 现在,并非绑定集合中的所有项目都有与之关联的网站,对于那些我希望删除带有标记ONLINE的MenuItem( 最后一个MenuItem)的项目。

But I can't find a way of achieving this. 但我无法找到实现这一目标的方法。

I've also thought of adding the ContextMenu and all the MenuItems programmatically so that I could conditionally add each MenuItem but I can't seem to find an event such as OnItemDataBound or OnDataBinding, etc . 我还想过以编程方式添加ContextMenu和所有MenuItems,以便我可以有条件地添加每个MenuItem,但我似乎无法找到OnItemDataBound或OnDataBinding 事件。

Can anyone help me out? 谁能帮我吗?

UPDATE: just to be more specific, I don't want to remove a MenuItem from the ContextMenu on every bound item in the ListBox, I only want to remove a MenuItem on specific items in the ListBox when the bound object fails a condition. 更新:更具体一点,我不想在ListBox中的每个绑定项上从ContextMenu中删除MenuItem,我只想在绑定对象失败条件时删除ListBox中特定项的MenuItem。

Let's say my ListBox contains 3 bound items: ListItem_1, ListItem_2, ListItem_3 假设我的ListBox包含3个绑定项:ListItem_1,ListItem_2,ListItem_3

Now, let's say that the object bound to ListItem_2 has one of its properties as NULL; 现在,假设绑定到ListItem_2的对象的一个​​属性为NULL; In this case ListItem_2 should have one of the MenuItem controls from its ContextMenu removed, whereas ListItem_1 and ListItem_3 should still have this MenuItem. 在这种情况下,ListItem_2应该删除其ContextMenu中的一个MenuItem控件,而ListItem_1和ListItem_3应该仍然具有此MenuItem。

What would be ideal is an event which allows me to capture each item as it's being bound, and which also exposes the ContextMenu. 理想的是一个事件,它允许我捕获每个项目,因为它被绑定,并且还暴露了ContextMenu。 The ListBox.Items collection simply returns the same collection of objects that I assigned to be the data source, whereas a collection of, say, ListBoxItems would be more useful - does this exist? ListBox.Items集合只返回我指定为数据源的相同对象集合,而ListBoxItems的集合将更有用 - 这是否存在?

First, give a name to your context menu to be able to retrieve it easily from the code-behind: 首先,为您的上下文菜单命名,以便能够从代码隐藏中轻松检索它:

<toolkit:ContextMenu x:Name="ContextMenu">

From there, you can access the items using this.ContextMenu.Items . 从那里,您可以使用this.ContextMenu.Items访问项目。 So just remove the ones you don't need anymore: 所以只需删除不再需要的那些:

var item = this.ContextMenu.Items.OfType<MenuItem>().First(m => (string)m.Tag == "ONLINE");

this.ContextMenu.Items.Remove(item);

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

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