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"
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
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>
Thank you for responding, excuse me what should I do in "
YourType
" makes me an error there ?, How would I get a message of "Item 3
" for example in the content of dialog by right clicking on "Item 3
"?YourType
is the type of the items in yourListView
's ItemsSource.I have the following problem: Screenshot problem
@fausdev I have edited the answer, please take a look.
Amazing works great !! 😁😁 Thank you very much, I will implement it in my project 👌👌