Question

Using a MSDN sample, I've managed to get a ListPicker Color Selector and I've added it to my project. It's using ObservableCollection.

The code for that is:

public class ColorSelectModel
    {
        public ColorSelectModel(string text, Color color)
        {
            this.Text = text;
            this.Color = color;
            this.ColorBrush = new SolidColorBrush(color);
        }
        public string Text { get; set; }
        public Color Color { get; set; }
        public SolidColorBrush ColorBrush { get; set; }
    }

And to load the Colors:

var colors = new List<Collections.ColorSelectModel>();
        colors.Add(new Collections.ColorSelectModel("Blue", Colors.Blue));
        colors.Add(new Collections.ColorSelectModel("Crimson", Colors.Brown));
        colors.Add(new Collections.ColorSelectModel("Cyan", Colors.Cyan));
        colors.Add(new Collections.ColorSelectModel("Gray", Colors.DarkGray));
        colors.Add(new Collections.ColorSelectModel("Dark Gray", Colors.Gray));
        colors.Add(new Collections.ColorSelectModel("Green", Colors.Green));
        colors.Add(new Collections.ColorSelectModel("Light Gray", Colors.LightGray));
        colors.Add(new Collections.ColorSelectModel("Magenta", Colors.Magenta));
        colors.Add(new Collections.ColorSelectModel("Orange", Colors.Orange));
        colors.Add(new Collections.ColorSelectModel("Purple", Colors.Purple));
        colors.Add(new Collections.ColorSelectModel("Red", Colors.Red));
        colors.Add(new Collections.ColorSelectModel("Yellow", Colors.Yellow));
        listPickerColor.ItemsSource = listPickerColor.ItemsSource ?? new ObservableCollection<Collections.ColorSelectModel>(colors);

So I've added this listpicker on Page 1. Everytime the user navigates to Page 1 this listpicker will automatically select the first value i.e. the color Blue. Now On another page, when the user clicks the Edit button, they will be navigated back to Page 1 where the values will be on the textboxes.

Now the problem I'm having is that I need a way to set the ListPicker to auto select the item depending on the value I read off using IsolatedStorage.

IsolatedStorageFileStream readColor = store.OpenFile("/color.txt", FileMode.Open, FileAccess.Read);
            using (StreamReader contactcolorx = new StreamReader(readColor))
            {
                var color = contactcolorx.ReadToEnd();
            }

Now the color will read fine. But I've tried: ListPicker.SelectectedItem = color; and ListPicker.SelectedIndex = color; Where when I tried the SelectedItem it will read the name and try to set it and Selected Index it will read the index number and try to set it but I have no luck.

Is anyone able to help me? Thanks!

Was it helpful?

Solution

Assuming you saved the index correctly to color.txt file, something like this should work :

var colorIndexString = contactcolorx.ReadToEnd();
var colorIndex = int.Parse(colorIndexString);
ListPicker.SelectedIndex = colorIndex;

of course, given ListPicker's ItemsSource populated first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top