0

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;
        }
    }

XAML

<Page
    x:Class="My_App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:My_App.Models"
    mc:Ignorable="d">

        <ListView ItemsSource="{x:Bind listItems}" ItemClick="ListView_ItemClick">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="data:ListItem">
                    <StackPanel>
                        <TextBlock Text="{Binding Book}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
</Page>
wbk727
  • 8,017
  • 12
  • 61
  • 125
  • You can access `ClickedItem` property of `ItemClickEventArgs` and Cast it to your `Book` object and from there you can check the Name. Is there any reason you are not using DataBinding? It would be much easier if you were using DataBinding. – Suresh May 31 '20 at 20:14
  • Could you not use Binding to `SelectedItem`? – Suresh May 31 '20 at 20:19
  • That is what I'm using. Not seen any tutorial with example code on this. – wbk727 May 31 '20 at 20:19
  • I don't know the correct way to use that. – wbk727 May 31 '20 at 20:39
  • Post full XAML, so that I understand how you are setting DatacContext, etc. Normally, you would just do `SelectedItem={Binding SelectedBook}` on your ListView. – Suresh May 31 '20 at 20:57

1 Answers1

0

You need to set IsItemClickEnabled property to true to get ItemClick event from the ListView.

Refer to docs page for ItemClick Event.

Here is the updated XAML would look like:

<ListView ItemsSource="{x:Bind listItems}" ItemClick="ListView_ItemClick" IsItemClickEnabled="True">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="data:ListItem">
            <StackPanel>
                <TextBlock Text="{Binding Book}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And the event handler code:

void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
    // TODO: Add your code by accessing e.ClickedItem here
}
Suresh
  • 4,091
  • 28
  • 35
  • @MacaronLover, you just need to one more thing in Suresh's code. Check if the item clicked is item A in the SelectionChanged handler. Like ' if (book.Name.Equals("Item A")) ' Then you could do what you want when the specific item is clicked. – Roy Li - MSFT Jun 01 '20 at 02:21
  • @MacaronLover updated answer as per previous comment. – Suresh Jun 01 '20 at 06:36
  • Why the `SelectionChanged` handle rather than the `ItemClick` handler? – wbk727 Jun 06 '20 at 15:32
  • @MacaronLover updated my answer to use `ItemClick` instead of `SelectionChanged` – Suresh Jun 07 '20 at 08:03