Augusto Accorsi
Augusto Accorsi

Reputation: 327

Get ListViewItem text by index WP RT

How do I get a ListViewItem text by this item index? I tried this

 txt = historico.Items[i].ToString();

but I got this

"Windows.UI.Xaml.Controls.ListViewItem" 

Upvotes: 0

Views: 242

Answers (3)

Kishor Bikram Oli
Kishor Bikram Oli

Reputation: 1848

In the MainPage.xaml:

<Page
x:Class="PushNotification_Sampl.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PushNotification_Sampl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <ListView Name="list">
        <ListViewItem>1</ListViewItem>
        <ListViewItem>2</ListViewItem>
        <ListViewItem>3</ListViewItem>
        <ListViewItem>4</ListViewItem>
        <Button x:Name="getBtn" Click="getBtn_Click">Get</Button>
        <TextBlock x:Name="text" Width="137" Text="check"></TextBlock>
    </ListView>



</Grid>

And in MainPage.xaml.cs, I have added the click event for the button:

private void getBtn_Click(object sender, RoutedEventArgs e)
    {

        string result = (list.Items[2] as ListViewItem).Content.ToString();
        text.Text = result;

    }

This works well in Windows Phone 8.1 app. Successfully tested. Hope this helps.

Upvotes: 1

Anthony Wieser
Anthony Wieser

Reputation: 4611

txt = (historico.Items[i] as ListViewItem).Content.ToString();

or

txt = (historico.Items[i] as ListViewItem).Text;

Upvotes: 1

Tim Freese
Tim Freese

Reputation: 435

txt = historico.Items[i].Text;

Upvotes: 0

Related Questions