从sqlite数据库中选择数据并将值绑定到windows phone 8应用程序中的列表框项目

我正在尝试用数据库(sqlite)技术创建windows phone 8项目

我尝试过以下方法:

1)从服务器下载sqlite fie并将其保存在本地应用程序中

2)我也可以从本地sqlite数据库中检索值,并使用此链接和其他一些链接中的一些示例代码显示在消息框中。

但是,我无法在列表框中绑定这些值。

这是我的代码:

XAML代码:

                                   

我的CS代码是:

  string DB_PATH = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sample.sqlite"); //SQLite connection private SQLiteConnection dbConn; private List _source = new List { "Full Schedule","Afghanistan","Australia","Bangladesh","England","Hong Kong","India","Ireland","Nepal","Netherlands","New Zealand","Pakistan","South Africa","Sri Lanka","UAE","West Indies","Zimbabwe" }; public MainPage1() { InitializeComponent(); selectTeam.ItemsSource = _source; } private void scheduleListbox_SelectionChanged(object sender, SelectionChangedEventArgs e) { dbConn = new SQLiteConnection(DB_PATH); /// Create the table Task, if it doesn't exist. dbConn.CreateTable(); /// Retrieve the task list from the database. List retrievedTasks = dbConn.Query("select * from iccworldt20_schedule").ToList(); /// Clear the list box that will show all the tasks. scheduleListbox.Items.Clear(); foreach (var t in retrievedTasks) { MessageBox.Show(t.ToString()); } } } public class iccworldt20_schedule { [PrimaryKey, AutoIncrement] public int match_id { get; set; } public string team1_Name { get; set; } public string team2_Name { get; set; } public string match_no { get; set; } public string group { get; set; } public string venue { get; set; } public string time { get; set; } public string day { get; set; } public override string ToString() { return team1_Name + ":" + team2_Name +venue; } } 

请提供一些解决方案,了解如何从sqlite DB中检索值并将其值绑定到列表框。

我看不到任何将数据添加到ListBox的代码。 如果从数据库中提取数据,请将其添加到ItemsSource属性中。

 scheduleListbox.ItemsSource = retrievedTasks; 

我不明白为什么在selectTeam ListPicker中使用ItemsSource =“{Binding}”时,在构造函数中填充它。 为什么你使用相同的scheduleListbox。 请参见绑定概述

如果您使用空绑定语法:{Binding}。 ListBox从父元素inheritanceDataContext。 如果未指定路径,则默认为绑定到整个对象。

我得到了它。根据“Jan Smuda”的回复,我找到了解决方案。

我删除了listpicker和listbox的XAML代码中的绑定语法(ItemsSource =“{Binding}”),并在代码本身中添加itemsSource。

所以我的XAML代码是这样的:

                                  

和我的CS代码是这样的:

  string country = "Full Schedule"; List ScheduleList; // the local folder DB path string DB_PATH = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sample.sqlite"); //SQLite connection private SQLiteConnection dbConn; ProgressIndicator _progressIndicator = new ProgressIndicator(); private List _source = new List { "Full Schedule","Afghanistan","Australia","Bangladesh","England","Hong Kong","India","Ireland","Nepal","Netherlands","New Zealand","Pakistan","South Africa","Sri Lanka","UAE","West Indies","Zimbabwe" }; public Schedule() { InitializeComponent(); selectTeam.ItemsSource = _source; Loaded += Schedule_Loaded; } void Schedule_Loaded(object sender, RoutedEventArgs e) { } private void selectTeam_SelectionChanged(object sender, SelectionChangedEventArgs e) { country = (sender as ListPicker).SelectedItem.ToString(); dbConn = new SQLiteConnection(DB_PATH); /// Create the table Task, if it doesn't exist. dbConn.CreateTable(); if (country == "Full Schedule") { ScheduleList = dbConn.Query("select * from tableName").ToList(); } else { ScheduleList = dbConn.Query("select * from tableName where team1_Name=? or team2_Name=?", country).ToList(); } scheduleListbox.ItemsSource = ScheduleList; } } public class match_schedule { [PrimaryKey, AutoIncrement] public int match_id { get; set; } public string team1_Name { get; set; } public string team2_Name { get; set; } public string match_no { get; set; } public string group { get; set; } public string venue { get; set; } public string time { get; set; } public string day { get; set; } } 

最后,我必须从本地SQLite数据库中检索列值,然后将其绑定到列表框中。

非常感谢jan Smuda回复我,感谢堆栈溢出。,