Items collection must be empty before using ItemsSource.
-
Tuesday, April 05, 2011 7:55 PM
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:
What am I doing wrong?<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>
All Replies
-
Tuesday, April 05, 2011 7:56 PMThe zero in the ListBox name was a typo...
-
Tuesday, April 05, 2011 8:12 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- Marked As Answer by ForestRanchSteve Tuesday, April 05, 2011 8:26 PM
-
Tuesday, April 05, 2011 8:25 PMThat worked. Thank you!!!!

