Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi Everyone,
I am developing windows mobile 7 application using c# in silverlight.
I need to take all the items I have in a array and input them into an listbox using c#

I am developing application that fetch the string from web service an stored in array named as mygroup(specified in the code).I need to display it in listbox.

here is my code:

for(int i = 0; i < userDetails.GroupDetails.Count; i++)
            {
               mygroup[i]= userDetails.GroupDetails[i];
              // this.listBox1.ItemsSource = mygroup[i].GroupName;
         // MessageBox.Show(Convert.ToString(mygroup[i].GroupName));

            }


Please Help me.

Thanks in Advance.
Posted
Updated 17-May-11 3:14am
v2

From the looks of your commented code, you don't have an understanding of how Silverlight itemscontrols are used.

You may want to work on learning Silverlight programming first...

Silverlight[^]
 
Share this answer
 
Use

<ListBox ItemsSource="{Binding}" x:Name="listBox1" ></ListBox>

in your XAML and in your code, put

this.listBox1.DataContext = collection;



Note : Collection could be better suited if you use an ObservableCollection so that if you add up an element later on the same collection, it will update your UI automatically.

Thats all.
 
Share this answer
 
v2
Comments
Mark Salsbery 17-May-11 19:54pm    
Question...How does setting the DataContext to a single item in the array populate the ListBox?
Abhishek Sur 18-May-11 2:55am    
Oops. You found my bug. I thought myGroup does not hold a single value. Of course you need to pass an Enumerable, may be better to use ObservableCollection to the DataContext to make it work.
Mark Salsbery 18-May-11 2:57am    
:) Arrays are IEnumerable too, but yes I agree there's better collection types to use.
Abhishek Sur 18-May-11 3:07am    
Yes, Actually Binding in XAML gives you a better chance to handle your data. If you are using MVVM patter, you can always store your Collection object and update the UI whenever required. Arrays are good, but they dont follow the rules with Binding, say it need to invoke INotifyCollectionChanged otherwise it needs to bind the whole Listbox again (means creating the Visual again altogether) Rather if you use a Collection which has INotifyCollectionChanged implemented, the control is capable of updating itself in part.

Yes arrays will work but only when the data is somewhat static for the current run.
Mark Salsbery 18-May-11 3:40am    
I assume that reply was meant for me but I'm glad you posted it to the OP...(s)he needs to learn that stuff!
thanks all for your replies.

"this.listBox1.DataContext = mygroup[i]"

it works ,but am getting only single value(last value of the loop) in my list box.it's not populating all the content from my data source "mygroup[i]".
 
Share this answer
 
Comments
Mark Salsbery 18-May-11 1:50am    
Why is this posted as a solution?

You only get a single value because that's exactly what you set the DataContext as. I have no idea why multiple posted solutions said to do that.

There's no reason this shouldn't work unless there's something you're not showing...

for(int i = 0; i < userDetails.GroupDetails.Count; i++)
{
mygroup[i]= userDetails.GroupDetails[i];
}
this.listBox1.ItemsSource = mygroup;
// or this (if XAML is <ListBox Name="listBox1" ItemsSource="{Binding}" >)
//this.listBox1.DataContext = mygroup;


This works (XAML looks like this: <ListBox Name="listBox1" />):

string[] mygroup = new string[10];
for(int i = 0; i < 10; i++)
{
mygroup[i]= i.ToString();
}
this.listBox1.ItemsSource = mygroup;

thanks all for your replies.

"this.listBox1.DataContext = mygroup[i]"

it works ,but am getting only single value(last value of the loop) in my list box.it's not populating all the content from my data source "mygroup[i]".
help me out this
 
Share this answer
 
Try this.listBox1.DataContext = mygroup[i]; instead of ItemsSource. ItemsSource is used for binding in your XAML file.

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900