How to get the item that I kept in the list

I am working with listview control in win8. I want to add an event when I hold an element and delete the element.

xaml code and events as follows:

<ListView x:Name="ImageList" VerticalAlignment="Bottom" Background="LightGray" Width="1050" BorderBrush="Black" BorderThickness="2" Grid.Column="1" Holding="ListView_Hold1" SelectionChanged="OnSelectedChanged" SelectionMode="Single" Height="152" ScrollViewer.HorizontalScrollBarVisibility="Auto" ItemContainerStyle="{StaticResource ListViewItemStyle1}" Style="{StaticResource ListViewStyle1}"> <ListView.ItemTemplate> <DataTemplate> <Image Opacity="0.7" Width="150" Height="125" Stretch="UniformToFill" Source="{Binding}" /> </DataTemplate> </ListView.ItemTemplate> <ListView.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ListView.ItemsPanel> </ListView> private async void ListView_Hold1(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs e) {...} 

It seems that I cannot get the information from holdroutdEventArgs, but the attribute is originalsource. But this is an image and in no way access to iteml

I found a relative question: "how to get a clicked item in a list." it can be solved by receiving the selecteditem attribute.

Can anybody help me? give me a hint.

+6
source share
2 answers

You can get it from HoldingRoutedEventArgs.OriginalSource.DataContext in your case: (Assuming ListView.ItemSource is an ImageModel list)

 private async void ListView_Hold1(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs args) { var source = (FrameworkElement)args.OriginalSource; var imageModel = (ImageModel)source.DataContext; } 
+8
source

You can get the index of an element using the SelectedIndex property (but for this to work, you need to select by pressing and holding the element)

 int i = imageList.SelectedIndex; 

So, to remove an element, you can use the RemoveAt () method

 imageList.Items.RemoveAt(i); 
0
source

Source: https://habr.com/ru/post/951799/


All Articles