Reputation: 37
I am new to windows phone 8 development. I have a set of value in sqlite database. Now want to show all the value in list view. C#code
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 code:
<ListBox Margin="0,365,0,0" x:Name="mylist1">
<ListBoxItem Height="190">
<TextBlock Width="443" Height="55" x:Name="txt1"></TextBlock>
</ListBoxItem>
</ListBox>
I show the value in message box. but I want to show the value in list view
Upvotes: 0
Views: 124
Reputation: 9242
this method you will going to need in your future code because sometimes you can not access control from thr name because they can be present in data templates..
first make a observablecollection/list of your messages strings and add your strings/texts in it. like this..
public ObservableCollection<string> MessageList { get; set; }
now add value this collection like this..
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"];);
now in your class contructor set the datacontext of the page..like this.
public AllMessages()
{
this.DataContext = this;
}
now bind set your listbox itemssource in xaml like this..
<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>
Upvotes: 1
Reputation: 878
You just need to add items too the listbox.
while (await statement.StepAsync())
{
mylist1.Items.Add(statement.Columns["Name"]);
}
Upvotes: 0