theoooood
theoooood

Reputation: 9

Get name of item in ListView

I am trying to have name of item of ListView in TextBlock, i really need to have: When i click on item, in TextBlock will be name of this item. For example: When i (in running) application click on item in ListView, that name will be in that TextBlock. Also names of items in ListView are declaring in running application.Thank you !!! I have only this simple code:

private void lstViewOfUsers_ItemClick(object sender, ItemClickEventArgs e)
{
} 

Upvotes: 0

Views: 2126

Answers (4)

Lam Le
Lam Le

Reputation: 1839

First, please tell us more details about how you bind items to your ListView.
Assuming you have:

    <ListView x:Name="MyListView"
              IsItemClickEnabled="True" 
              ItemsSource="{x:Bind MyItems}"
              ItemClick="MyListView_ItemClick">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:Item">
                <TextBlock x:Name="MyTextBlock" Text="{x:Bind Name}"></TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

And in the code behind it:

    private List<Item> MyItems;
    //Fill items of your need into this List

My approach is to create an identifier of each item you fill in the ListView. The identifier can be of any type you want. Here I use an enum. Remember to set the Category for every item in MyItems above:

    public class Item
    {
        public string Name{ get; set; }
        public ItemCategory Category { get; set; }
    }

    public enum ItemCategory
    {
        Type1,
        Type2
    }

And the main work in MyListView_ItemClick method:

     private void MenuItemsListView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var clickedItem = (Iteme)e.ClickedItem;
        switch (clickedItem.Category)
        {
            case ItemCategory.Type1:
            { 
                //Do your works here
                break;
            }

            case ItemCategory.Type2:
            { 
                //Do your works here
                break;
            }
        }
    }

Upvotes: 0

Tea Nguyen
Tea Nguyen

Reputation: 78

Try this:

private void lstViewOfUsers_ItemClick(object sender, ItemClickEventArgs e)
{
     myTextBlock.Text =((Control)sender).Name;
}

Upvotes: 2

Jayden
Jayden

Reputation: 3286

If you want use the ItemClick event, you can use the ItemClickEventArgs.ClickedItem to get a reference to the clicked item.

If you add the ListViewItem by the XAML, you should be able to convert ItemClickEventArgs.ClickedItem to the root element in ListViewItem.

For example:

<ListView Name="MyListView" ItemClick="ListView_ItemClick" IsItemClickEnabled="True">
    <ListViewItem>
        <TextBlock Name="Text1" Text="Text1"></TextBlock>
    </ListViewItem>
    <ListViewItem>
        <TextBlock Name="Text2" Text="Text2"></TextBlock>
    </ListViewItem>
</ListView>

The code behind:

private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
    var textBlock = e.ClickedItem as TextBlock;
    Debug.WriteLine(textBlock.Text);
}

If you bind the Collection to the ListView.Source, we should be able to convert ItemClickEventArgs.ClickedItem to the object of the Collection.

For example:

<ListView Name="MyListView" ItemClick="ListView_ItemClick" IsItemClickEnabled="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding }"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The code behind:

private ObservableCollection<string> Items;

public MainPage()
{
    this.InitializeComponent();
    Items = new ObservableCollection<string>();
    Items.Add("Text1");
    Items.Add("Text2");
    MyListView.ItemsSource = Items;
}

private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
    var text = e.ClickedItem as string;
    Debug.WriteLine(text);
}

Upvotes: 2

user2950509
user2950509

Reputation: 1057

It's quite difficult to understand what exactly you are talking about, but it seems that what you may be looking for is:

private void lstViewOfUsers_ItemClick(object sender, ItemClickEventArgs e)
{
     myTextBlock.Text = e.DataContext as string;
}

If you aren't adding plain strings to the listview, but a custom object, you can do myTextBlock.Text = (e.DataContext as myClass).myString

Upvotes: 0

Related Questions