use*_*701 3 contextmenu menuitem-selection windows-phone-7
首先,我知道这个主题:如何使上下文菜单适用于Windows Phone?
但这种方式很复杂......所以我有这个XAML代码:
 <StackPanel Name="friendsGrid" Margin="0,0,0,0" Background="Transparent"> 
   <ListBox Name="friendsListBox" FontSize="32" Tap="friendsListBox_Tap">
     <toolkit:ContextMenuService.ContextMenu>
     <toolkit:ContextMenu Name="MyContextMenu" Opened="MyContextMenu_Opened">
     <toolkit:MenuItem Header="action" Click="contextMenuAction_Click"/>
     </toolkit:ContextMenu>
     </toolkit:ContextMenuService.ContextMenu>
   </ListBox>
 </StackPanel>
我正在填写这样的列表:
this.friendsListBox.Items.Add(friend.serviceName);
但是,当然,当我执行longtap时,会出现上下文菜单并选择整个List,而不仅仅是一个项目.
是否有一些简单的方法来识别项目被轻敲?谢谢
顺便说一句,我找到了这个方法,但contextMenuListItem没有收到任何东西,它仍然是null:
ListBoxItem contextMenuListItem = friendsListBox.ItemContainerGenerator.ContainerFromItem((sender as ContextMenu).DataContext) as ListBoxItem;
ibo*_*kiy 14
您应该将ContextMenu块放入ItemTemplate(而不是ListBox块).这里简短的样本.
XAML:
            <ListBox Name="TestList" Margin="26,0,26,0" Height="380" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}">
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu Name="ContextMenu" >
                                <toolkit:MenuItem Name="Edit" Header="Edit" Click="Edit_Click"/>
                                <toolkit:MenuItem Name="Delete"  Header="Delete" Click="Delete_Click"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
码:
    public List<string> Items = new List<string>
    {
        "Item1",
        "Item2",
        "Item3",
        "Item4",
        "Item5",
    };
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        TestList.ItemsSource = Items;
    }
    private void Edit_Click(object sender, RoutedEventArgs e)
    {
        if (TestList.ItemContainerGenerator == null) return;
        var selectedListBoxItem = TestList.ItemContainerGenerator.ContainerFromItem(((MenuItem) sender).DataContext) as ListBoxItem;
        if (selectedListBoxItem == null) return;
        var selectedIndex = TestList.ItemContainerGenerator.IndexFromContainer(selectedListBoxItem);
        MessageBox.Show(Items[selectedIndex]);
    }
希望这可以帮助.
| 归档时间: | 
 | 
| 查看次数: | 7349 次 | 
| 最近记录: |