The problem with your code is that you assign the same property ItemsContainerStyle that impacts all of the ListBoxItems. So all of the items will have the color of the last one.
Your code should directly assign the Background of the items.
for (int i = 0; i < ListBox_SavedColors.Items.Count; i++)
{
//Get the color and store it in a brush.
Color color = App.SavedColors[ListBox_SavedColors.Items[i].ToString()];
SolidColorBrush backgroundBrush = new SolidColorBrush(color);
ListBoxItem item = ListBox_SavedColors.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
item.Background = backgroundBrush;
}
But as Peter Hansen it is better to use a XAML to solve this problem. For exemple you can write a converter that will convert the data in the ListBoxItem into a SolidColorBrush. Then you can bind the background of an item to its content.
public class ListBoxItemColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new SolidColorBrush(App.SavedColors[value.ToString()]);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML will look like this:
<ListBox x:Name="MyList">
<ListBox.Resources>
<local:ListBoxItemColorConverter x:Key="ListBoxItemColorConverter"/>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="{Binding Converter={StaticResource ListBoxItemColorConverter}}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>