locked
How to get the holding item in a ListView

    Question

  • Hello,

    How to get the holding item in a ListView, I tried this code inside Holding Event but it is not working.

    var myOjbect = (sender as ListView).DataContext as MyClass;

    Thanks in advance.

    Tuesday, June 24, 2014 12:36 PM

Answers

  • Hi,

    You can define a Listview/GridView DataTemplate like below:

    <ListView x:Name="listview" 
                           ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ListViewItem Holding="ListViewItem_Holding" IsHoldingEnabled="True" >
                            <Image  Source="{Binding Image}" />
    
                        </ListViewItem>
                    </DataTemplate>
                </ListView.ItemTemplate>
                
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
            </ListView>

    In XAML above, you can define listviewitem Holding event and set its IsHoldingEnabled to true, that value determines whether the Holding event can originate from that element. Then you can get listviewitem in the holding event:

     private void ListViewItem_Holding(object sender, HoldingRoutedEventArgs e)
            {
                var tt = sender;
            }
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by zee_patel Wednesday, June 25, 2014 5:37 AM
    Wednesday, June 25, 2014 1:44 AM

All replies

  • your code will get the list view's data context, not the item.

    If the item is selected, locate ListView.SelectedItem, or SelectedIndex to selected item.

    If you override the default listitem's datatemplate, you can add a Holding event handler and get the sender that way.

    Tuesday, June 24, 2014 3:20 PM
  • Hi,

    You can define a Listview/GridView DataTemplate like below:

    <ListView x:Name="listview" 
                           ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ListViewItem Holding="ListViewItem_Holding" IsHoldingEnabled="True" >
                            <Image  Source="{Binding Image}" />
    
                        </ListViewItem>
                    </DataTemplate>
                </ListView.ItemTemplate>
                
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
            </ListView>

    In XAML above, you can define listviewitem Holding event and set its IsHoldingEnabled to true, that value determines whether the Holding event can originate from that element. Then you can get listviewitem in the holding event:

     private void ListViewItem_Holding(object sender, HoldingRoutedEventArgs e)
            {
                var tt = sender;
            }
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by zee_patel Wednesday, June 25, 2014 5:37 AM
    Wednesday, June 25, 2014 1:44 AM