2
votes

I am having an issue when trying to bind an object I created to a listpicker. I have had success using a listpicker with strings and ints but I am running into issues when trying to use my own class/object.

Here is the XML (I have two listpickers, one works and one doesn't)

                            <toolkit:ListPicker 
                            x:Name="CountryListPicker" 
                            Margin="0,2,0,10" Width="458" 
                            BorderThickness="3" FullModeHeader="Country"
                            CacheMode="BitmapCache">
                            <toolkit:ListPicker.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Border Background="LightBlue" Width="34" Height="34">
                                            <TextBlock Text="{Binding _code}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                        <TextBlock Text="{Binding _name}" Margin="12 0 0 0"/>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:ListPicker.ItemTemplate>
                            <toolkit:ListPicker.FullModeItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" Margin="0 21 0 20">
                                        <TextBlock Text="{Binding _name}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:ListPicker.FullModeItemTemplate>
                        </toolkit:ListPicker>

                        <toolkit:ListPicker
                            x:Name="testPicker"
                            Header="Accent color" 
                            FullModeHeader="ACCENTS" 
                            CacheMode="BitmapCache">
                            <toolkit:ListPicker.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding}" Margin="12 0 0 0"/>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:ListPicker.ItemTemplate>
                            <toolkit:ListPicker.FullModeItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" Margin="0 21 0 20">
                                        <TextBlock Text="{Binding}"
                                   Margin="16 0 0 0"
                                   FontSize="43"
                                   FontFamily="{StaticResource PhoneFontFamilyLight}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:ListPicker.FullModeItemTemplate>
                        </toolkit:ListPicker>`

The first list picker is my new object binding one and the second is binding a string.

Here is the code behind:

        ObservableCollection<Country> countries = new ObservableCollection<Country>();
        countries.Add(new Country { _code = "US", _name = "United States1"});
        countries.Add(new Country { _code = "US", _name = "United States2" });
        countries.Add(new Country { _code = "US", _name = "United States3" });
        countries.Add(new Country { _code = "US", _name = "United States4" });
        countries.Add(new Country { _code = "US", _name = "United States5" });
        countries.Add(new Country { _code = "US", _name = "United States6" });
        this.CountryListPicker.ItemsSource = new ReadOnlyCollection<Country>(countries);

        ObservableCollection<string> _accentColors = new ObservableCollection<string>();
        _accentColors.Add("Blue");
        _accentColors.Add("Blue2");
        _accentColors.Add("Blue3");
        _accentColors.Add("Blue4");
        _accentColors.Add("Blue5");
        _accentColors.Add("Blue6");
        _accentColors.Add("Blue7");
        this.testPicker.ItemsSource = new ReadOnlyCollection<string>(_accentColors);

The 2nd listpicker is fine and I think this is because it contains strings.

My broken listpicker displays all of the correct data but when I select a listpickeritem, it doesn't select it. The app loads the full listpicker and I click one of the items and when I return back to the main view the selected item isn't reflected in the listpicker.

Anyone have any ideas? Need me to explain anymore?

1
Hi, there's a new version of wp7 toolkit and it has fixed A LOT of bugs in its ListPicker, you might wanna go check it out. :) silverlight.codeplex.com/releases/view/75888Justin XL
Thanks, but I am using the latest. I thought updating would fix it but it only fix scrolling issues. Does my object have to implement some sort of interface to inform the UI that a selection has occurred?garry
I also posted this issue on CodePlex for those of you who want to follow this. I will post any answers on here if they work for me. silverlight.codeplex.com/workitem/9839garry
Sorry just saw your comments, good that you have it figured out. I think it makes sense. When you navigate back to your main page, the ListPicker gets loaded again and resets its ItemsSource and that's why it loses its SelectedIndex.Justin XL

1 Answers

2
votes

I figured this one out on my own. I had a Loaded function where the listpicker was initializing. Looks like Loaded gets "loaded" when the listpicker returns from the fullscreen view which in turn resets the selectedIndex. Fix was easy, just move the creation of the lists as well as setting the itemSource from the Loaded function into the constructor.