none
Items collection must be empty before using ItemsSource.

    Question

  • 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?

    Tuesday, April 05, 2011 7:55 PM

Answers

  • 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:12 PM

All replies

  • The zero in the ListBox name was a typo...
    Tuesday, April 05, 2011 7:56 PM
  • 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:12 PM
  • That worked. Thank you!!!!
    Tuesday, April 05, 2011 8:25 PM