Search code examples
c#uwplistboxlistboxitem

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()


Solution

  • You may want to try:

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