Search code examples
windows-phone-7listpicker

ListPicker windows phone 7 giving error


I have list picker in my windows phone 7 app & it works fine but whenever i try to add more options in it... it crashes i can add upto 4 or 5 options only in this list picker is there anything i can do aboout it

My working code is

xaml code

                <toolkit:ListPicker x:Name="ListPicker" Margin="12,3,12,12" Foreground="#FF00C000" >
                    <toolkit:ListPicker.Items>
                        <toolkit:ListPickerItem Content="Item1"/>
                        <toolkit:ListPickerItem Content="Item2"/>
                    </toolkit:ListPicker.Items>
                </toolkit:ListPicker>

& .cs code

            string ListPickerOperator = (this.ListPicker.SelectedItem as ListPickerItem).Content as string;

            switch (ListPickerOperator )
            {
                case "Item1":

                    break;

                case "Item2":

                    break;
            }

but whenever i try to make this list bigger it crashes after 4 5 items


Solution

  • I think you should use item template. You couldn't show more than 4 or 5 items because you have to use "Full Mode template". to use item template you should do something like this.

    <toolkit:ListPicker x:Name="ListPicker" Margin="12,3,12,12" Foreground="#FF00C000" SelectionChanged="ListPicker_SelectionChanged" >
    
            <!--Normal Item template-->
            <toolkit:ListPicker.ItemTemplate>
             <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
    
            <!--Full Mode template-->
            <toolkit:ListPicker.FullModeItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </toolkit:ListPicker.FullModeItemTemplate>
        </toolkit:ListPicker>
    

    To populate the listpicker you only have to use itemSource propetary

            //list string 
            List<String> itemsList = new List<String>();
    
            // create 100 items 
            for (int j = 0; j < 100; j++)
            {
                itemsList.Add("item" + j);
            }
    
            //itemsource
            this.ListPicker.ItemsSource = itemsList;
    

    to get the selectedItem you could do something like this

    String ListPickerOperator= ((String)this.ListPicker.SelectedItem);
    
            switch (ListPickerOperator)
            { 
    
                case "item1":
                    MessageBox.Show("item 1 was selected");
                    break;
                case "item2":
                    MessageBox.Show("item 2 was selected");
                    break;
                    /*
                     .
                     * .
                     * .
                     * .
                     * .
                  */
    
            }