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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21956476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:59:15  来源:igfitidea点击:

select data from sqlite database and binding values to listbox item in windows phone 8 apps

c#wpfsqlitewindows-phone-8

提问by Punniyam Moorthi

I am trying to creating windows phone 8 project with database (sqlite) techniques

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

I have tried the following:

我尝试了以下方法:

1) Download sqlite fie from server & saved it in local app

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

2) I can also retrieve the values from local sqlite database and displayed in message box by using some sample codes from This Link& some other links too.

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

But, I am not able to bind that values in listbox.

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

Here is my code :

这是我的代码:

XAML code :

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>

and my CS code is:

我的 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;
    }
}

Please give some solution that how to retrieve values from sqlite DB and bind its values to listbox.,

请给出一些解决方案,说明如何从 sqlite DB 检索值并将其值绑定到列表框。,

采纳答案by Jan Smuda

I can't see any code where you are adding data to a ListBox. If you have data pulled from the database, add it to ItemsSource property that's all.

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

scheduleListbox.ItemsSource = retrievedTasks;

I do not understand why you use ItemsSource="{Binding}" on selectTeam ListPicker when you fill it in the constructor. And why do you use the same to scheduleListbox. See Binding overview

我不明白你为什么在 selectTeam ListPicker 上使用 ItemsSource="{Binding}" 在构造函数中填充它。以及为什么您使用相同的来 scheduleListbox。请参阅绑定概述

If you youse empty binding syntax: {Binding}. The ListBox inherits the DataContext from a parent element. When the path is not specified, the default is to bind to the entire object.

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

回答by Punniyam Moorthi

i got it., according to "Jan Smuda " reply i found solution for that .,

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

i removed the binding syntax (ItemsSource="{Binding}") in XAML code for both listpicker and listbox and add itemsSource in code itself., like this

我删除了列表选择器和列表框的 XAML 代码中的绑定语法 (ItemsSource="{Binding}"),并在代码本身中添加了 itemsSource。,就像这样

so my XAML code like this:

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

   <ListBox Name="scheduleListbox" Margin="5,85,5,60" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Height="100" Width="480" Margin="0,0,0,5" Background="CadetBlue">
                        <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding team1_Name}" Name="team1Name" Foreground="White"></TextBlock>
                        <TextBlock Grid.Row="1" Text="{Binding team2_Name}" Name="team2Name" Foreground="Red"></TextBlock>
                        <TextBlock Grid.Row="2" Text="{Binding venue}" Name="venue" Foreground="Yellow"></TextBlock>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <toolkit:ListPicker Name="selectTeam" Height="60" 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>

and my CS code like this :

和我的 CS 代码是这样的:

    string country = "Full Schedule";
    List<match_schedule> 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<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 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<match_schedule>();

        if (country == "Full Schedule")
        {
            ScheduleList = dbConn.Query<match_schedule>("select * from tableName").ToList<match_schedule>();
        }
        else
        {
           ScheduleList = dbConn.Query<match_schedule>("select * from tableName where team1_Name=? or team2_Name=?", country).ToList<match_schedule>();
        }
        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; }
}

Finally I Got to retrieve column values from my local SQLite Database and i bind it in listbox.,

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

Lot of thanks to jan Smuda for responding me and thanks to stack overflow also.,

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