question

ApptacularApps-8305 avatar image
ApptacularApps-8305 asked ·

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

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;
     }
 }
windows-uwpwindows-uwp-xaml
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Have you solved your issue? Does @yanxiaodi 's answer help? Please feel free to contact us if you still have questions.

0 Votes 0 · ·

1 Answer

yanxiaodi avatar image
yanxiaodi answered ·

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.

Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.