Search code examples
c#windows-phone-7silverlight-4.0crashlistpicker

Windows Phone 7 - C# How to use ListPickerItem in code without it crashing?


Listpickeritem thus far seems to be a bit of a head ache insofar that it crashes very easily.

I have the following code which crashes when the listpickeritem is selected while running the app:

for (int i = 1; i <= 100; i++)
    {
         ListPickerItem item = new ListPickerItem();
         item.Content = i.ToString() + "%";
         item.FontSize = 35;
         listPicker1.Items.Add(item); 
    }

XAML:

    <toolkit:ListPicker Name="listPicker1" Margin="251,117,92,-93" Width="113" FontSize="40">
                </toolkit:ListPicker>

Does anyone know of a way to set the fontsize throuch c# without it crashing? If I manually enter the xaml, that also causes crashes and I don't want to have to type a long list when I can do it programmatically instead.

Edit Answer below.

Error message that was originally coming up: Unhandled exception ">PivotApp1.dll!PivotApp1.App.Application_UnhandledException(object sender, System.Windows.ApplicationUnhandledExceptionEventArgs e) Line 125 + 0x5 bytes C#"


Solution

  • Basically what I was trying to do was create a listpicker list for percentage between 1 and 100 for a user to select. It seems like a lot of code for a simple task, but unfortunately there wasn't any other way to modify the listpicker fontsize. The following code was the solution I came up with to solve my problem:

    XAML:

    <toolkit:ListPicker x:Name="listPicker1" 
                        Margin="251,117,92,-93" 
                        Width="113" >
                        <toolkit:ListPicker.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Percent}" FontSize="40" />
                            </DataTemplate>
                        </toolkit:ListPicker.ItemTemplate>
                        <toolkit:ListPicker.FullModeItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Percent}" FontSize="40" />
                            </DataTemplate>
                        </toolkit:ListPicker.FullModeItemTemplate>
                    </toolkit:ListPicker>
    

    Created a new class called Percentage.cs

    public class Percentage
    {
        public int Percent
        {
            get;
            set;
        }
    
    }
    

    Then from within my class that I wanted to control the listpicker from, I added the following code to create a list of 1 to 100 using a list created from the Percentage class:

       List<Percentage> percentage = new List<Percentage>();
       for(int i = 1; i <= 100; i++)
       {
            percentage.Add(new Percentage() { Percent = i });
    
       }
       this.listPicker1.ItemsSource = percentage;