你好,我有一个加载到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();
}
提前致谢
我的原始答案不正确,因此我决定对其进行编辑。
首先,创建一个名为场_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>
谢谢您的答复,对不起,我该怎么办
YourType
?”,如何Item 3
通过右键单击“Item 3
” 在对话框的内容中获得“ ” 消息?YourType
是您ListView
的ItemsSource中项目的类型。我有以下问题:屏幕截图问题
@fausdev我已经编辑了答案,请看一看。
很棒的作品!😁😁非常感谢,我将在我的项目中实现它👌👌