简体   繁体   中英

Select ListBoxItem under RightTapped event

I have the following event right now

private void contactGrid_RightTapped(object sender, RightTappedRoutedEventArgs e)
{         

    if (contactGrid.SelectedIndex >= 0)
    {    
        FrameworkElement senderElement = sender as FrameworkElement;

        MenuFlyout menu = new MenuFlyout();
        MenuFlyoutItem item1 = new MenuFlyoutItem() { Text = "Edit Contact" };
        MenuFlyoutItem item2 = new MenuFlyoutItem() { Text = "Comfirm" };
        MenuFlyoutSubItem item2a = new MenuFlyoutSubItem() { Text = "Remove Contact" };

        item1.Click += new RoutedEventHandler(EditContactClicked);
        item2.Click += new RoutedEventHandler(RemoveContactClicked);

        item2a.Items.Add(item2);


        menu.Items.Add(item1);
        menu.Items.Add(item2a);

        menu.ShowAt(senderElement, e.GetPosition(contactGrid));
    }
}

This works fine and creates the right click context menu at the mouse pointer on top of a listbox item, but only if it has been selected first. What I can't figure out is how to get the RightTapped event to select the item that was right tapped. I have yet to test this in tablet mode and I'm currently using a mouse to trigger the right tapped event (by right clicking).

Is the default behaviour of a long press (to trigger a right tap) in tablet mode such that it selects the item anyway?

As far as I understand, contactGrid is your ListBox ? I guess you have any kind of List or Collection set as the ItemsSource of the ListBox? Then you can set the SelectedItem property in your right tapped-event like following:

First you need to modify the ItemTemplate, so that RightTapped belongs to a ListBoxItem:

<ListBox x:Name="ContactGrid">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Background="Transparent" RightTapped="contactGridItem_RightTapped">
                <TextBlock Text="{Binding}" />
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And in code (I'm actually wondering that the Flyout is shown above the selectedItem and not above the whole ListBox):

private void contactGridItem_RightTapped(object sender, RightTappedRoutedEventArgs e)
{         
    FrameworkElement senderElement = sender as FrameworkElement;

    // Now you can get the tapped Item from the DataContext and set is as selected
    contactGrid.SelectedItem = senderElement.DataContext;

    if (contactGrid.SelectedIndex >= 0)
    {    

        MenuFlyout menu = new MenuFlyout();
        MenuFlyoutItem item1 = new MenuFlyoutItem() { Text = "Edit Contact" };
        MenuFlyoutItem item2 = new MenuFlyoutItem() { Text = "Comfirm" };
        MenuFlyoutSubItem item2a = new MenuFlyoutSubItem() { Text = "Remove Contact" };

        item1.Click += new RoutedEventHandler(EditContactClicked);
        item2.Click += new RoutedEventHandler(RemoveContactClicked);

        item2a.Items.Add(item2);


        menu.Items.Add(item1);
        menu.Items.Add(item2a);

        menu.ShowAt(senderElement, e.GetPosition(contactGrid));
    }
}

(not tested, but that's how I would solve it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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