SunXiaoqiang
SunXiaoqiang

Reputation: 71

How to get the item I was holding in the listview

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

the xaml and event code like this:

<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 can not get any information from holdingroutdEventArgs but the attribute of originalsource. But it is the image and no way access to iteml

I have found a relative question: "how to get the clicked item in the listview". it can be solved by getting the attribute of selecteditem.

anyone can help me ? give me some clue.

Upvotes: 6

Views: 1736

Answers (2)

Lars Udengaard
Lars Udengaard

Reputation: 1267

You should be able to get it from the HoldingRoutedEventArgs.OriginalSource.DataContext, in your case: (Assuming that the ListView.ItemSource is a list of ImageModel)

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

Upvotes: 8

Augusto Accorsi
Augusto Accorsi

Reputation: 327

You can get the index of the item using the SelectedIndex property (but for these work you have to select, by pressing, and after hold the item)

int i = imageList.SelectedIndex;

So to delete the item you can use the RemoveAt() method

imageList.Items.RemoveAt(i);

Upvotes: 0

Related Questions