Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:21:12+00:00 2026-05-29T15:21:12+00:00

my xaml file <ListBox Height=522 HorizontalAlignment=Left Margin=20,162,0,0 Name=listBox1 VerticalAlignment=Top Width=448 ItemsSource={Binding}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel

  • 0

my xaml file

<ListBox Height="522" HorizontalAlignment="Left" Margin="20,162,0,0" Name="listBox1" VerticalAlignment="Top" Width="448" ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Text}" Foreground="#FFC8AB14" FontSize="36" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

xaml.cs file

       listBox1.Items.Clear();
       for (int i = 0; i < tasks.Count(); i++) {
            List<Taskonlistbox> dataSource = new List<Taskonlistbox>();
            dataSource.Add(new Taskonlistbox() {Text = "Blalalalala"} );
            this.listBox1.ItemsSource = dataSource; // visual stdio shows error here:
        }

Taskonlistbox:

public class Taskonlistbox
{
    public string Text { get; set; }
}

Error: “Items collection must be empty before using ItemsSource”
whats a problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:21:14+00:00Added an answer on May 29, 2026 at 3:21 pm

    You want to create the list only once and assign the data source only once! Therefore, create the list before the loop and assign the data source after the loop

    // Clear the listbox.
    // If you never add items with listBox1.Items.Add(item); you can drop this statement.
    listBox1.Items.Clear();
    
    // Create the list once.
    List<Taskonlistbox> dataSource = new List<Taskonlistbox>(); 
    
    // Loop through the tasks and add items to the list.
    for (int i = 0; i < tasks.Count(); i++) { 
        dataSource.Add(new Taskonlistbox {Text = "Blalalalala"} ); 
    }
    
    // Assign the list to the `ItemsSouce` of the listbox once.
    this.listBox1.ItemsSource = dataSource;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xaml file with this code: <GridViewColumn x:Name=lvCol3 Header=Quantità Width=120> <GridViewColumn.CellTemplate> <DataTemplate>
Here are the relevant parts of the XAML file: xmlns:local=clr-namespace:BindingTest <ListBox x:Name=myList ItemsSource={Binding Source={x:Static
In my XAML file, I have a ListBox declared like this : <ListBox x:Name=lstDeck
This is the xaml of listview <ListView Grid.Column=1 Height=auto Name=ListView1 Width=auto AllowDrop =True >
<ListBoxItem Content=A /> </ListBox> -> above is my listbox in xaml file -> how
I have a ListBox in a file called Downloads.XAML. The items bound to that
I have a .xaml file and a .cs file that share value with Binding.
In my XAML file I want to display this text which contains double and
I have a XAML file that I exported from Expression Design. I would like
I have a simple WPF (XAML) file that has some animated shapes and text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.