Items collection must be empty before using ItemsSource !!!
-
Hi, I have written the below code for display some data in a ListView, but whenever I run it I receive this Exception
Items collection must be empty before using ItemsSource
public class RandomTreeViewItems
{
public string name { get; set; }
public string tag { get; set; }
public string location { get; set; }
}public partial class MainWindow : Window
{
public ObservableCollection<RandomTreeViewItems> randomTreeViewItems = new ObservableCollection<RandomTreeViewItems>();public MainWindow() { this.InitializeComponent(); this.DataContext = randomTreeViewItems; addItems(); } private void addItems() { for (int i = 0; i < 100; i++) { randomTreeViewItems.Add(newItem()); } } RandomTreeViewItems newItem() { return new RandomTreeViewItems { name = DateTime.Now.Ticks.ToString(), location = DateTime.Now.Ticks.ToString(), tag = DateTime.Now.Ticks.ToString() }; }
}
<Grid x:Name="LayoutRoot">
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" />
<Label Content="{Binding Path=name}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Tags" Width="200" DisplayMemberBinding="{Binding Path=tag}" />
<GridViewColumn Header="Location" Width="400" DisplayMemberBinding="{Binding Path=location}" />
</GridView>
</ListView.View>
<Separator />
</ListView>
</Grid>Could you please guide me, what's wrong with it? thanks
-
Hi, I have written the below code for display some data in a ListView, but whenever I run it I receive this Exception
Items collection must be empty before using ItemsSource
public class RandomTreeViewItems
{
public string name { get; set; }
public string tag { get; set; }
public string location { get; set; }
}public partial class MainWindow : Window
{
public ObservableCollection<RandomTreeViewItems> randomTreeViewItems = new ObservableCollection<RandomTreeViewItems>();public MainWindow() { this.InitializeComponent(); this.DataContext = randomTreeViewItems; addItems(); } private void addItems() { for (int i = 0; i < 100; i++) { randomTreeViewItems.Add(newItem()); } } RandomTreeViewItems newItem() { return new RandomTreeViewItems { name = DateTime.Now.Ticks.ToString(), location = DateTime.Now.Ticks.ToString(), tag = DateTime.Now.Ticks.ToString() }; }
}
<Grid x:Name="LayoutRoot">
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" />
<Label Content="{Binding Path=name}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Tags" Width="200" DisplayMemberBinding="{Binding Path=tag}" />
<GridViewColumn Header="Location" Width="400" DisplayMemberBinding="{Binding Path=location}" />
</GridView>
</ListView.View>
<Separator />
</ListView>
</Grid>Could you please guide me, what's wrong with it? thanks
The separator you are adding in the xaml is being treated as an item, remove it or place it in the grid instead.
-
The separator you are adding in the xaml is being treated as an item, remove it or place it in the grid instead.
Great, It works ;) Thanks