How to check which string resource of a ListItem in a ListView was clicked?

Apptacular Apps 386 Reputation points
2020-05-31T20:11:50.1+00:00

Is there a way to check the string resource of ListView item click rather than index of the ListView (which is often demonstrated in tutorials)? e.g. I have a ListView with 3 items: "Item A", "Item B" and "Item C". If the string resource of the clicked ListView item is "Item A", do something.

MainPage class

public sealed partial class MainPage : Page
{
    private List<ListItemMain> mainItems;

    public MainPage()
    {
        this.InitializeComponent();

        gridItemMains = MainItemManger.GetGridItems();
    }

    private void ListView_ItemClick(object sender, ItemClickEventArgs e)
    {

    }
}

Item class

public class ListItem
{
public string Name { get; set; }
}

public class BookManger
{
    public static List<Book> GetListItems()
    {
        var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();

        var items = new List<Book>
        {
            new Book { Name = resourceLoader.GetString("ItemA") },
            new Book { Name = resourceLoader.GetString("ItemB") },
            new Book { Name = resourceLoader.GetString("ItemC") }
        };

        return items;
    }
}
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Xiaodi Yan 876 Reputation points MVP
    2020-05-31T20:47:05.817+00:00

    Hi, it's actually similar with the index. If you have set the data-binding for the ListView, the clicked item should be your Book model. So you can get the selected Book instance and check its Name property. eg:

    private void ListView_ItemClick(object sender, ItemClickEventArgs e)
     {
         // You can make a global field for the below resourceLoader
         var resourceLoader = ...
         Book item = (Book)e.ClickedItem;
         if (item.Name == resourceLoader.GetString("ItemA"))
         {
              //do something
         }
     }
    

    Please let me know if it doesn't work.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.