Listpicker和返回值

时间:2012-04-09 15:18:48

标签: c# windows-phone-7 listpicker

我是这种语言的新手用户。我读了这个方法 http://windowsphonegeek.com/articles/listpicker-for-wp7-in-depth 但我仍有这个问题:

 // 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; }
               }

我创建了一个listpicker并插入了值,现在我创建了这个button1:

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

我没有选择当天但是无效的投射错误,我不知道选择了返回值。 你能救我吗?

1 个答案:

答案 0 :(得分:2)

您将SampleData放入ListPicker。如果您想要“Day”属性,则应使用

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