Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Items collection must be empty before using ItemsSource !!!

Items collection must be empty before using ItemsSource !!!

Scheduled Pinned Locked Moved WCF and WF
csswpfwcftutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    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

    I 1 Reply Last reply
    0
    • M Mohammad Dayyan

      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

      I Offline
      I Offline
      Insincere Dave
      wrote on last edited by
      #2

      The separator you are adding in the xaml is being treated as an item, remove it or place it in the grid instead.

      M 1 Reply Last reply
      0
      • I Insincere Dave

        The separator you are adding in the xaml is being treated as an item, remove it or place it in the grid instead.

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        Great, It works ;) Thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups