Title: How to Add multiple item in list Windows Phone
Tags: c#;windows-phone-8
Question: I am working on windows phone app and i want to add item in already exists list i the problem is that when i have added first item its ok but when i am adding second item the list is replace my previous added item
private void btn_profession_Loaded(object sender, RoutedEventArgs e)
try
{
selectedKeywordsIds = App.skillIds;   // for selected keyword
selectedProfName = App.professionalName;
selectedProfId = App.professionalId;
this.btn_profession.Content = selectedProfName;
Key = App.skillKeywords  ;
Pro = App.professionalName;
final1=Key.Replace(Pro, "");
List<string&gt; numbers = final1.Split(',').ToList<string&gt;();
numbers.Add(selectedKeywordsIds);
numbers = numbers.Where(s =&gt; !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
listBox1.ItemsSource = null;
listBox1.ItemsSource = numbers;
Comment: sir when i will add second item it will move on to next index

Comment: So here you call add only once... if the newly added item is the same as one already existing on the list the "Distinct()" is an issue. If you call the add second time somehere else show that place.

Comment: Ah so you mean that in the control when displayed the new item replaces the preivously added? Maybe you should just go with numbers.Insert(0, newitem); Then you do not append at the end of collection but at the beginning of the collection? Or use some sorting on collection or on the control for display?

Comment: Well, you ARE using distinct, which means that every item will occur only once.


Here is another answer: Sounds like you want the same list item to be displayed again meaning you should not use Distinct() on the numbers after filtering for null or white spaces.
numbers  = numbers.Where(s =&gt; !string.IsNullOrWhiteSpace(s)).ToList();

Here is another answer: It looks like you are building the list from some previous variable final1 each time before adding item. You should consider something like this
final1 = string.Join(',', numbers.ToArray());
after you add new item. So the changes done to "numbers" collection are stored back as comma separated items in your final1 variable.
Comment for this answer: Przemysław Ładyński sir actually i am getting the value from another page and store it on a string value then add it on list but when i will add second item the previous added item is replaced by me item
Comment for this answer: Can you show the piece of code where you add the second item? I am pretty sure you are somehow overriding your collection in the meantime. Otherwise just calling: numbers.Add(first); numbers.Add(second); should never overwrite the previous item.

Here is another answer: Try using an ObservableCollection-
ObservableCollection<string&gt; coll = new ObservableCollection<string&gt;(numbers);
Set the itemsource of the ListBox to this observable collection.
You will not need to use-
listBox1.ItemsSource = null;
listBox1.ItemsSource = numbers;
Just add or remove the items you want from the observable collection. The changes will reflect on the listBox1 in UI automatically.
Also, you would probably need to change the code -
numbers = numbers.Where(s =&gt; !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
to something more appropriate for observable collection.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:05/29/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><windows-phone-8>', 'Title': 'How to Add multiple item in list Windows Phone', 'ViewCount': 106, 'id': 30530221, 'user_AboutMe': None, 'user_AccountId': 6099643.0, 'user_DisplayName': 'user', 'user_Reputation': 125.0, 'user_Views': 26.0, 'user_id': 4759091.0}}