How to get clicked item in ListView

一个人想着一个人 提交于 2019-12-17 16:39:15

问题


This should be a trivial Task but I can't find how to do it. I want to listen to a click on an item in a listview, get the corresponding model object, and launch a new screen.

This is the XAML for the ListView:

<ListView x:Name="ItemListView" 
    ItemTemplate="{StaticResource StoreFrontTileTemplate}"
    ItemContainerStyle="{StaticResource StoreFrontLVTileStyle}"
    Margin="0" VerticalAlignment="Top" ItemClick="MyClick" Tapped="MyTap"/>

And MyClick, MyTap:

private void MyClick(object sender, ItemClickEventArgs e)
{
    Debug.WriteLine("Click!");
}

private void MyTap(object sender, TappedRoutedEventArgs e)
{
    Debug.WriteLine("TAp!!" + sender.ToString() + "--" + e.ToString());
}

The method to navigate to the new screen:

this.Frame.Navigate(typeof(SecondScreen));

It works, but I need the model object of the clicked item and pass it as a Parameter to the second screen.

But MyClick is never called, and MyTap doesn't give me any Information about the clicked item. "sender" is the ListView.

I downloaded these exaples:

http://code.msdn.microsoft.com/windowsapps/XAML-ListView-sample-pack-0ec6b60f

But it doesn't contain what I need, there's a master / detail view, but it works with bindings and what I want to do is launch a complete new screen.

Note: I'm a noob in Windows development and orienting to the usual way to do it in Android or IOS where you implement a callback with the position of the clicked element. No idea about the right way to do it in Windows 8.


回答1:


You can use the SelectionChanged event:

<ListView x:Name="ItemListView" SelectionChanged="MySelectionChanged" />

And you can get the selected/deseleted items from the SelectionChangedEventArgs e.g.:

private void MySelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Debug.WriteLine("Selected: {0}", e.AddedItems[0]);
}

Or if you don't need the selection functionality and what to use the ItemClick="MyClick" you need to set IsItemClickEnabled to true:

Gets or sets a value that indicates whether items in the view raise an ItemClick event in response to interaction.

<ListView x:Name="ItemListView" 
    ItemTemplate="{StaticResource StoreFrontTileTemplate}"
    ItemContainerStyle="{StaticResource StoreFrontLVTileStyle}"
    Margin="0" VerticalAlignment="Top" ItemClick="MyClick"  
    IsItemClickEnabled="bool" SelectionMode="None"/>

Note that in this case you also need to set the SelectionMode to None.




回答2:


You can use DoubleTapped event of listview on XAML code. And then, in C# code you can get position by:

private void display_DoubleTapped_1(object sender,
     Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
     int items = display.SelectedIndex;
     // use this index to do something 
}



回答3:


I use this:

private void ListUI_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if(ListUI.SelectedItems.Count != 0)
        {
            Debug.WriteLine("It's not a trap at all my friend");
        }
        else
        {
            Debug.WriteLine("Its a trap");
        }
    }



回答4:


       // We have a class name 'Message' in ControllerLibrary Namespace

         namespace ControllerLibrary
            {
              public class Message
                {
                   public string MessageID { get; set; }
                    public string MessageName { get; set; }
                 }
            }

    // Add name space at page heading
               xmlns:local1="using:ControllerLibrary"

      //At Gridview 

        <GridView IsItemClickEnabled="True" Name="UserMessageList">
         <GridView.ItemTemplate>
           <DataTemplate x:DataType="local1:Message">                                          <StackPanel Orientation="Horizontal">
         <Button Content="{x:Bind MessageName}" HorizontalAlignment="Left" Margin="2 0 10 0" Click="btnUserMessage_Click">
        </Button>
        </StackPanel>
        </DataTemplate>
         </GridView.ItemTemplate>
        </GridView>

  //At code Behind
    private void btnAddPage_Click(object sender, RoutedEventArgs e)
      {
          //Get selecte message
          var selectedMessage = (sender as Button).DataContext as Message;
      }



回答5:


I would suggest that, in the code-behind C# file associated to the XAML, you use the ClickedItem property of the ItemClickEventArgs object passed as argument to the ItemClick event handler.

Example (for a GridView, but it's the same idea for a ListView):

private void GridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            // This is how you determine the clicked item in a GridView,
            // and obtain the relevant element of the underlying collection
            // (to which the GridView is bound).
            // 'Tile' is here the 'type' used in the said collection.
            Tile output = e.ClickedItem as Tile;

            // ... 

        }


来源:https://stackoverflow.com/questions/13326583/how-to-get-clicked-item-in-listview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!