温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - UWP
c# mvvm uwp uwp-xaml xaml

c# - 超人

发布于 2020-05-04 03:41:46

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

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

在此处输入图片说明

我有以下示例代码:

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

    }

提前致谢

查看更多

提问者
fausdev
被浏览
19
Washington A. Ramos 2020-02-16 12:29

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

首先,创建一个名为场_selectedValue与你的类型ListView的的ItemsSource项目“类型,我称之为“MyClass的”:

private MyClass _selectedItem;

然后,注册您的的RightTapped事件ListView

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

从那里,从获取DataContext RightTappedRoutedEventArgs

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>