Warm tip: This article is reproduced from stackoverflow.com, please click
c# mvvm uwp uwp-xaml xaml

UWP

发布于 2020-05-03 11:49:16

hello I have a list of data (sqlite) loaded to a listview and everything works marravilla if I select a listviewItem and right click on it, but I want to get the current listviewitem (under the pointer) without selecting any listviewitem

what I want is similar to the application of "Microsoft to DO"

enter image description here

and I have the following sample code:

MainPage.xaml

<Grid>
    <ListView x:Name="myList">
        <ListViewItem>Item 1</ListViewItem>
        <ListViewItem>Item 2</ListViewItem>
        <ListViewItem>Item 3</ListViewItem>
        <ListViewItem>Item 4</ListViewItem>
        <ListViewItem>Item 5</ListViewItem>

        <ListView.ContextFlyout>
            <MenuFlyout x:Name="itemActual">
                <MenuFlyoutItem Text="see" Click="MenuFlyoutItem_Click"/>
            </MenuFlyout>
        </ListView.ContextFlyout>
    </ListView>

</Grid>

MainPage.xaml.cs:

 private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
    {
        ContentDialog dialog = new ContentDialog()
        {
            //Content = myList.item ????

            PrimaryButtonText = "ok"
        };
        dialog.ShowAsync();

    }

Thanks in advance

Questioner
fausdev
Viewed
28
Washington A. Ramos 2020-02-16 12:29

My original answer wasn't correct so I decided to edit it.

First, create a field called _selectedValue with the type of your ListView's ItemsSource items' type, I'll call it "MyClass":

private MyClass _selectedItem;

Then, register the RightTapped event of your ListView:

<ListView x:Name="myList" RightTapped="myList_RightTapped">

From there, get the DataContext from the RightTappedRoutedEventArgs:

private void myList_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e) {
    _selectedItem = (e.OriginalSource as FrameworkElement).DataContext as MyClass;
}

When your flyout's Click event is fired, use _selectedValue:

private void MenuFlyoutItem_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
    // Do stuff with _selectedValue
}

Full example files:

MainPage.cs:

 public sealed partial class MainPage : Page {
    #region Fields
    private List<MyClass> _items;
    private MyClass _selectedItem;
    #endregion

    public MainPage() {
        this.InitializeComponent();

        _items = new List<MyClass>();
        _items.Add(new MyClass() { Name = "O" });
        _items.Add(new MyClass() { Name = "P" });
        myList.ItemsSource = _items;
    }

    private void MenuFlyoutItem_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
        // Do stuff with _selectedValue
    }

    private void myList_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e) {
        _selectedItem = (e.OriginalSource as FrameworkElement).DataContext as MyClass;
    }

    public class MyClass {

        public string Name { get; set; }

        public override string ToString() => Name;

    }
}

MainPage.xaml:

<Page
    x:Class="UWP.Sandbox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWP.Sandbox"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <ListView x:Name="myList" RightTapped="myList_RightTapped">
            <ListView.ContextFlyout>
                <MenuFlyout x:Name="itemActual">
                    <MenuFlyoutItem Text="see" Click="MenuFlyoutItem_Click"/>
                </MenuFlyout>
            </ListView.ContextFlyout>
        </ListView>
    </Grid>
</Page>