ItemCollection must be empty before using itemsource

5 posts, 0 answers
  1. Daniel
    Daniel avatar
    9 posts
    Member since:
    Mar 2011

    Posted 22 May 2011 Link to this post

    I am getting this error "ItemCollection must be empty before using itemsource" when assigning the itemsource property of a listbox on the Popupopening event.

    The XAML looks like

                <telerikPrimitives:RadPickerBox x:Name="pikCommList" Margin="6,10,201,558" Content="Change Email/Fax" PopupOpening="GetCommData_Popup" >

       <ListBox x:Name="lisComm"  Background="Black" Padding="20" >           
                      
             <TextBlock  Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" Text="{Binding CommName}" Name="txtCommName"/>
                
              
        </ListBox>
                    
        </telerikPrimitives:RadPickerBox>

    I an querying the data in the PopupOpening Event handler and assigning the itemsource like this



            private void GetCommData_Popup(object sender, object CancelEventArgs)
            {
                
           

                var q = (from x in lDataAdapter.GetTable<ChronicleDA.PERSONCOMM>()
                         where x.PERSONID == ActiveContext.Person.PersonID
                         join c in lDataAdapter.GetTable<ChronicleDA.COMMUNICATIONS>()
                         on x.COMMUNICATIONID equals c.COMMUNICATIONID
                         join e in lDataAdapter.GetTable<ChronicleDA.EMAIL>()
                         on c.COMMUNICATIONID equals e.COMMUNICATIONID       
                         select new PersonComm { PersonID = x.PERSONID, CommTypeID = c.SYSTEMTYPEID, CommID = c.COMMUNICATIONID, CommName = e.EMAILFULLADDRESS}).Take(100);


                IQueryable[] Queries = new IQueryable[] { q };
                lDataAdapter.BeginExecute(
                   Queries,
                   delegate(IAsyncResult ar)
                   {
                       if (ActiveContext.ActiveQuery.QueryRunning == false) return;
                       ActiveContext.ActiveQuery.QueryRunning = false;
                       try
                       {
                           lDataAdapter.EndExecute(ar);
                           Dispatcher.BeginInvoke(() =>
                           {
                               var tt = q.ToList<PersonComm>();

                               lisComm.ItemsSource =   tt;



    If I remove this line from the XAML the code works fine but I do not have the textbox in the listbox assigned as I want.  What do I need to do to correct this?

      <TextBlock  Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" Text="{Binding CommName}" 

    Thanks for your help
    Daniel


  2. Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 23 May 2011 Link to this post

    Hi Daniel,

    Thanks for contacting us.

    The reason for the exception you are getting is that you have already added an item in the Items collection of your ListBox by inserting the TextBlock instance between the ListBox tags in your XAML code. This is the one way to populate a ListBox instance with items. The other way is to use the ItemsSource property and assign an IEnumerable instance to it. You cannot use both properties simultaneously to populate the control with items.

    So, in your case you first add the TextBlock instance from the XAML code and after that bind the ItemsSource property to a collection of items which causes an exception because you already have an item added via XAML.

    To solve this problem you should either remove the TextBlock from the ListBox in your XAML code, or simply use the ItemsSource property to populate the control with items.

    I hope this helps.

    Do not hesitate to get back to us in case you have further questions or need assistance.



    Greetings,
    Deyan
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. DevCraft banner
  4. Daniel
    Daniel avatar
    9 posts
    Member since:
    Mar 2011

    Posted 23 May 2011 Link to this post

    So if I chose to just use the itemsource property is there any way to format how the items will appear?  
  5. Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 24 May 2011 Link to this post

    Hi Daniel,

    Thanks for writing back and for your question.

    The System.Windows.Controls.ListBox class inherits from System.Windows.Controls.ItemsControl which defines the ItemTemplate property. By using this property you can define how your items will appear in the control. You can read more about this property here:

    http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx

    I hope this helps.

    Do not hesitate to get back to us in case you have further questions or need assistance.

    All the best,
    Deyan
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  6. Daniel
    Daniel avatar
    9 posts
    Member since:
    Mar 2011

    Posted 24 May 2011 Link to this post

    Deyan,

    That worked great.  Thanks for all the help

    Daniel
Back to Top
DevCraft banner