在列表框中显示一组值

时间:2013-07-24 12:49:25

标签: c#-4.0 windows-phone-8

我是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"];
            } 

xaml代码:

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

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

2 个答案:

答案 0 :(得分:1)

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

首先制作一个observablecollection / list of messages messages strings并在其中添加你的字符串/文本。喜欢这个..

  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"];);

现在在你的类contructor中设置页面的datacontext ..就像这样。

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

现在绑定在xaml中设置你的listbox itemssource就像这样..

<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>

答案 1 :(得分:0)

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

while (await statement.StepAsync())
{
    mylist1.Items.Add(statement.Columns["Name"]);
}