Trey Balut
Trey Balut

Reputation: 1395

UWP ListBox ForEach

I'm trying to loop through a UWP ListBox.

My XAML:

 <ListBox x:Name="SeasonsListBox" Margin="786,24,558,863" HorizontalAlignment="Left" VerticalAlignment="Top" >
        <ListBoxItem Content="2016-2017"/>
        <ListBoxItem Content="2017-2018"/>
        <ListBoxItem Content="2018-2019"/>
    </ListBox>

And here is my code behind:

        var items = SeasonsListBox.Items;
        foreach (var item in items)
        {
            string temp = item.ToString();
        }

I'm trying to get the Content, but it doesn't allow me to item.Content.ToString()

Upvotes: 0

Views: 145

Answers (1)

Aly Elhaddad
Aly Elhaddad

Reputation: 1943

You may want to try:

string temp = (item as ListBoxItem).Content.ToString();

Upvotes: 2

Related Questions