Ant
Ant

Reputation: 281

Listpicker and return value

I am a newbie user of this language. I read this how-to http://windowsphonegeek.com/articles/listpicker-for-wp7-in-depth but I still have this question:

 // Constructor
    public MainPage()
    {
        InitializeComponent();
        List<SampleData> dataSource = new List<SampleData>();
        dataSource.Add(new SampleData() { Day = "Sunday"});
        dataSource.Add(new SampleData() { Day = "Monday"});
        dataSource.Add(new SampleData() { Day = "Tuesday"});
        dataSource.Add(new SampleData() { Day = "Thirsday"});
        dataSource.Add(new SampleData() { Day = "Wednesday"});
        dataSource.Add(new SampleData() { Day = "Friday" });
        dataSource.Add(new SampleData() { Day = "Saturday"});
        this.listPicker.ItemsSource = dataSource;
    }

    public class SampleData
    {
        public string Day { get; set; }
               }

I create a listpicker and I insert the value, now I create this button1:

private void button1_Click(object sender, RoutedEventArgs e)
{  
    String s;
    s=(String)listPicker.SelectedItem;
    MessageBox.Show(s);    
}

I don't have the day selected but an invalid cast error, I don't know return value selected. Can you help me?

Upvotes: 1

Views: 1141

Answers (1)

William Melani
William Melani

Reputation: 4268

You put SampleData into the ListPicker. If you want the 'Day' property, you should use

private void button1_Click(object sender, RoutedEventArgs e)
    {  
 var sampleData =(SampleData)listPicker.SelectedItem;
 var day = sampleData.Day;
MessageBox.Show(day);    
 }

Upvotes: 2

Related Questions