This is a migrated thread and some comments may be shown as answers.

ItemCollection must be empty before using itemsource

4 Answers 279 Views
PickerBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daniel
Top achievements
Rank 1
Daniel asked on 23 May 2011, 04:40 AM
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


4 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 23 May 2011, 12:08 PM
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>>
0
Daniel
Top achievements
Rank 1
answered on 23 May 2011, 06:09 PM
So if I chose to just use the itemsource property is there any way to format how the items will appear?  
0
Deyan
Telerik team
answered on 24 May 2011, 07:14 AM
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>>
0
Daniel
Top achievements
Rank 1
answered on 24 May 2011, 10:29 PM
Deyan,

That worked great.  Thanks for all the help

Daniel
Tags
PickerBox
Asked by
Daniel
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Daniel
Top achievements
Rank 1
Share this question
or