如何从列表框中选择项目更新数据库 - windows phone 7?

时间:2013-01-14 14:56:49

标签: c# windows-phone-7 linq-to-sql windows-phone-7.1

我正在开发一个需要更新操作的Windows Phone 7应用程序。我有以下表类。

[Table]
public class StudentTable
{

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int StudentID
    {
        get;
        set;
    }

    [Column (CanBeNull = false)]
    public string StudentName
    {
        get;
        set;
    }
    [Column(CanBeNull = false)]
    public string StudentClass
    {
        get;
        set;
    }
    [Column(CanBeNull = false)]
    public DateTime regDate
    {
        get;
        set;
    }
}

我使用以下方法为每个学生获取价值..

 using (StudentDataContext context = new StudentDataContext(strConnectionString))
        {

            StudentTable newStudent = new StudentTable
            {

                StudentName = textBox1.Text.ToString(),
                StudentClass = textBox2.Text.ToString(), 
                regDate = ((DateTime) datePicker.Value).Date.Add(((DateTime)timePicker.Value).TimeOfDay) 

            };

            context.StudentInfo.InsertOnSubmit(newStudent);
            context.SubmitChanges();

        }

并在列表框中显示学生....

                 <ListBox HorizontalAlignment="Left"  Name="StudentlistBox1" ItemsSource="{Binding}"  Loaded="StudentlistBox1_Loaded">
                 <ListBox.ItemTemplate>
                     <DataTemplate>
                         <StackPanel>
                               <TextBlock Text="{Binding StudentName}" FontSize="{StaticResource PhoneFontSizeLarge}"></TextBlock>
                               <TextBlock Text="{Binding StudentClass}" FontSize="{StaticResource PhoneFontSizeLarge}"></TextBlock>
                               <TextBlock Text="{Binding regDate}" FontSize="{StaticResource PhoneFontSizeSmall}"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

我尝试编码,以便列表框中的每个选定项目都会指向我可以更新所选记录的页面。 如果我得到所选项目的主键,我相信我可以这样做...如何选择列表框中的学生并传递该学生ID?任何想法.....我刚接触到Windows Phone 7 ..请帮助..

1 个答案:

答案 0 :(得分:0)

方法1: - 通过导航传递详细信息

String uri = "/Page2.xaml?param1="+value;
NavigationService.Navigate(new Uri(uri, UriKind.Relative));

在导航到方法的第2页中访问: -

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

像这样: - QueryString this.NavigationContext.QueryString["param"];

方法2: - 将ID存储在IsolatedStorage设置中

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings["paramFromPage1"]=paramValue;

使用appSettings [“paramFromPage1”]通过在第2页中正确解析它来检索它

* 方法3: - [可能在Application LifeTimeObjects !!中]