从sqlite数据库中选择数据并将值绑定到Windows Phone 8应用中的列表框项

Punniyam Moorthi

我正在尝试使用数据库(sqlite)技术创建Windows Phone 8项目

我尝试了以下方法:

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

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

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

这是我的代码:

XAML代码:

   <Grid x:Name="ContentPanel" Grid.RowSpan="2">
        <ListBox Name="scheduleListbox" Margin="5,85,5,60" ItemsSource="{Binding}" SelectionChanged="scheduleListbox_SelectionChanged">
             <ListBox.ItemTemplate>
                <DataTemplate>
                <Grid Height="250" Width="480">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20"></RowDefinition>
                        <RowDefinition Height="40"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="40"></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Name="team1Name" Text="{Binding team1_name}" Grid.Row="0"></TextBlock>
                    <TextBlock Name="team2Name" Text="{Binding team2_name}"></TextBlock>
                    <TextBlock Name="venue" Text="{Binding venue}" ></TextBlock>
                </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <toolkit:ListPicker Name="selectTeam" Height="60" ItemsSource="{Binding}" VerticalAlignment="Bottom" Background="Black" FullModeHeader="Select your Team Schedule" Foreground="White" ExpansionMode="FullScreenOnly" Margin="5,0" SelectionChanged="selectTeam_SelectionChanged">
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="listpickerStackpannel" >
                        <TextBlock Text="{Binding}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
            <toolkit:ListPicker.FullModeItemTemplate>
                <DataTemplate>
                    <StackPanel Name="listpickerStackpannel" Margin="10">
                        <TextBlock Text="{Binding}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.FullModeItemTemplate>
        </toolkit:ListPicker>
    </Grid>

而我的CS代码是:

     string DB_PATH = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sample.sqlite");
    //SQLite connection
    private SQLiteConnection dbConn;
    private List<string> _source = new List<string>
    {
        "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<iccworldt20_schedule>();
        /// Retrieve the task list from the database.
        List<iccworldt20_schedule> retrievedTasks = dbConn.Query<iccworldt20_schedule>("select * from iccworldt20_schedule").ToList<iccworldt20_schedule>();
        /// 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从父元素继承DataContext。如果未指定路径,则默认为绑定到整个对象。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

检查绑定到MVVM应用程序中的ObservableCollection的列表框中的选定项

数据绑定到int列表“ Windows Phone 8”

排序列表框(Windows Phone)

创建与每个列表框项一起显示的“自定义菜单” Windows Phone

即使在mvvm Windows Phone应用程序中返回后,列表框也会突出显示选定的项目

从“保留”事件中的“ Windows Phone”列表框中获取选定的项目

文本块下方的数据绑定列表框未在Windows Phone中滚动

在Windows Phone 8上使用SQLite-net创建SQLite数据库文件

用于刷新列表框Windows Phone中的项目的特定UI

如何在Windows Phone 8中制作不可滚动的列表框

如何滚动到Windows Phone 7列表框中的选定项目

为什么比Windows Phone 8中的列表框更喜欢LongListSelector

Windows Phone列表框ItemTemplate绑定到listBoxItem

Windows Phone 8 SQLite数据库升级

Windows Phone 8中未显示列表框数据和图像

什么控件用作Windows Phone 8上的内联列表框?

需要在Windows Phone 8中创建带有动画的列表框删除项

如何在Windows Phone绑定中从列表框获取值?

如何在Windows Phone 8中使用没有列表框的xml文件?

使用C#从Windows Phone的列表框中删除所选项目

从Windows Phone的弹出窗口中的数据绑定列表中选择一个项目

从Windows Phone 8的列表框中访问特定的文本块

列表框中的Windows Phone进度栏显示随机值

从SQLite数据库中的数据库中选择

如何在Windows Phone 8的字符串数组中的每个列表框项目上应用背景色?

数据绑定SQLIte到列表框(包含三个控件)Windows Phone 8

长列表选择器中的列表框命令未触发-Windows Phone 8-MVVM

列表框数据绑定Windows Phone

在 Windows Phone 8.1 应用程序中连接到 SQLite 数据库时出错