Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 491
SunXiaoqiang Avatar asked Aug 15 '13 10:08

SunXiaoqiang


1 Answers

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;
}
like image 140
Lars Udengaard Avatar answered Oct 30 '22 20:10

Lars Udengaard