0

我是 Windows Phone 8 开发的新手。我在 sqlite 数据库中有一组值。现在想在列表视图中显示所有值。C#代码

Database database = new Database(ApplicationData.Current.LocalFolder, "people.db");

            await database.OpenAsync();

            string query = "SELECT * FROM PEOPLE";
            Statement statement = await database.PrepareStatementAsync(query);

            statement.EnableColumnsProperty();



            while (await statement.StepAsync())
            {
                MessageBox.Show(statement.Columns["Name"] );

                txt1.Text = statement.Columns["Name"];
            } 

xml代码:

<ListBox Margin="0,365,0,0" x:Name="mylist1">
                <ListBoxItem Height="190">
                    <TextBlock Width="443" Height="55" x:Name="txt1"></TextBlock>
                </ListBoxItem>
            </ListBox>

我在消息框中显示值。但我想在列表视图中显示值

4

2 回答 2

1

您将在以后的代码中需要此方法,因为有时您无法从 thr 名称访问控制,因为它们可以存在于数据模板中。

首先制作您的消息字符串的可观察集合/列表,并在其中添加您的字符串/文本。像这样..

  public ObservableCollection<string> MessageList { get; set; }

现在像这样添加这个集合的价值..

        MessageList = new ObservableCollection<string>();
        MessageList.Add(statement.Columns["Name"];);
        MessageList.Add(statement.Columns["Name"];);
        MessageList.Add(statement.Columns["Name"];);
        MessageList.Add(statement.Columns["Name"];);
        MessageList.Add(statement.Columns["Name"];);

现在在你的类构造器中设置页面的数据上下文..像这样。

 public AllMessages()
    {
       this.DataContext = this;
    }

现在像这样在 xaml 中绑定设置您的列表框项目源..

<ListBox Margin="0,365,0,0" x:Name="mylist1" ItemsSource="{Binding MessageList}">
            <ListBoxItem Height="190">
                <TextBlock Width="443" Height="55" x:Name="txt1" Text="{Binding}"></TextBlock>
            </ListBoxItem>
        </ListBox>
于 2013-07-24T13:04:57.653 回答
0

您只需要在列表框中添加项目。

while (await statement.StepAsync())
{
    mylist1.Items.Add(statement.Columns["Name"]);
} 
于 2013-07-24T13:04:48.710 回答