Jump to content


WP8 / c# List.ItemsSource completed

c# windows phone 8

  • Please log in to reply
2 replies to this topic

#1 James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 20 August 2013 - 20:43

I am making a Windows Phone app and I need to select some of the items loading in the list.  I'm looping through some XML and adding them to a list

var TransitList = new List<string>();

//loop through XML here

BATList.ItemsSource = TransitList;

This works fine, however after this line I am attempting to set some of the item as selected

                SolidColorBrush brushSelected = new SolidColorBrush(Color.FromArgb(255, 190, 190, 190));

                for (int i = 0; i < this.BATList.Items.Count; i++)
                    ListBoxItem BATListItem = (ListBoxItem)(this.BATList.ItemContainerGenerator.ContainerFromIndex(i));
                    BATListItem.IsSelected = true;
                    BATListItem.Background = brushSelected;

However each item is coming up as null.  If the code is placed elsewhere an manually run (obviously not an option) the values are not null and the process runs correctly. 

So, I need one of two things to happen:

* I need an event to fire when the ItemsSource is completed

* A way to add values to the list that can be selected (obviously a string list wont do)





#2 ~Johnny



  • Joined: 10-August 08
  • Location: London, England, Earth, Milky Way

Posted 20 August 2013 - 21:07

            var list = new List<String> { "Cheese", "Hello", "Apples", "Fish" };

            lb.ItemsSource = list;
            lb.SelectionMode = SelectionMode.Multiple;
That does the job for me, using a bog standard ListBox
<ListBox x:Name="lb" />
You could, and probably should set the SelectionMode in XAML though :p
(The default ItemsContainer style for a ListBox overwrites the foreground colour of each ListBoxItem to the phones Accent colour - using Visual States -  when it / it's child becomes selected, which is why this works. It'll usually be easier to work with Visual States or change properties through binding converters rather than messing directly with trying to manipulate individual visual elements through code )

Ergo, to change the colour, you'd have to overwrite the default ItemsContainerStyle. Using Blend (or Visual Studio), you can right click on your ListBox in design time view, Click "Edit Additional Templates", click "Edit ItemsContainerStyle", store the resource somewhere and then look the XAML for the container style - you'll see a selected visual state that changes the foreground colour - you just stick your own colour in there.

#3 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 20 August 2013 - 21:19

Thanks, that is some help.  Have a great evening.

Click here to login or here to register to remove this ad, it's free!