你好,我有一个加载到listview的数据列表(sqlite),如果我选择一个listviewItem并右键单击它,一切都会正常进行,但是我想在不选择任何listviewitem的情况下获取当前listviewitem(在指针下方)

我想要的是类似于“Microsoft to DO”的应用程序

c# - UWP-如何通过右键单击或右击而没有选择Listviewitem来获取ListViewItem值?-LMLPHP

我有以下示例代码:

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();

    }

提前致谢

最佳答案

我的原始答案不正确,因此我决定对其进行编辑。

首先,使用_selectedValue的ItemsSource项目类型创建一个名为ListView的字段,我将其称为“MyClass”:

private MyClass _selectedItem;

然后,注册ListView的RightTapped事件:
<ListView x:Name="myList" RightTapped="myList_RightTapped">

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

触发弹出按钮的Click事件时,请使用_selectedValue:
private void MenuFlyoutItem_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
    // Do stuff with _selectedValue
}

完整的示例文件:

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>

关于c# - UWP-如何通过右键单击或右击而没有选择Listviewitem来获取ListViewItem值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60235693/

10-12 06:52