Items collection must be empty before using ItemsSource.

Answered Items collection must be empty before using ItemsSource.

  • Tuesday, April 05, 2011 7:55 PM
     
      Has Code

    I am new to WPF and am having a difficult time getting data binding to work. The following code using ListView works fine, but there seems to be no way to get rid of the column headings in the GridView:

    <ListView Name="lsvSelectedTerminals"
         Grid.Column="3"
         Grid.Row="1"
         ItemsSource="{Binding SelectedTerminals}">
       <ListView.View>
          <GridView >
            <GridView.Columns>
               <GridViewColumn Width="75" 
                       DisplayMemberBinding="{Binding Path=Term}" />
               <GridViewColumn Width="200"
                       DisplayMemberBinding="{Binding Path=Description}" />             
            </GridView.Columns>
          </GridView>
       </ListView.View>
    </ListView>
    
    
    this.DataContext = new MainWindowViewModel(); 
    

    If I try and change it to a ListBox, I get the error "Items collection must be empty before using ItemsSource:

     <ListBox Name="lsvSelecte0dTerminals"
         Grid.Column="3"
         Grid.Row="1"
         ItemsSource="{Binding SelectedTerminals}">
      <StackPanel Orientation="Horizontal">
         <TextBlock Width="75"
              Text="{Binding Path=Term}" />
         <TextBlock Width="200"
              Text="{Binding Path=Description}" />
      </StackPanel>
    </ListBox>
    
    What am I doing wrong?

All Replies

  • Tuesday, April 05, 2011 7:56 PM
     
     
    The zero in the ListBox name was a typo...
  • Tuesday, April 05, 2011 8:12 PM
     
     Answered Has Code
    You may change your listbox code as below
    <ListBox Name="lsvSelecte0dTerminals"
       Grid.Column="3"
       Grid.Row="1"
       ItemsSource="{Binding SelectedTerminals}">
    <ListBox.ItemTemplate>
            <DataTemplate>
              <StackPanel Orientation="Horizontal">
                <TextBlock Width="75"
         Text="{Binding Path=Term}" />
                <TextBlock Width="200"
         Text="{Binding Path=Description}" />
              </StackPanel>
    
            </DataTemplate>
          </ListBox.ItemTemplate>
    </ListBox>
    


    Ajosh Jose
  • Tuesday, April 05, 2011 8:25 PM
     
     
    That worked. Thank you!!!!