0

I'm trying to list out data from a local database to a list box for WP8.1

.xaml:

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
            <Image Source= "**Fruit Image**" Height="26" Width="50" Margin="12,10,9,0" VerticalAlignment="Top"/>
                <TextBlock Text="**Fruit Name**"  FontSize="26" Style="{StaticResource BodyTextBlockStyle}"/>
                <TextBlock Text="**Fruit Colour**"  FontSize="26" Style="{StaticResource BodyTextBlockStyle}"/>
        </StackPanel>
    </DataTemplate>
 </ListBox.ItemTemplate>

Getting value from my database

.cs:

public void ListFruit()
{
    using (var db = new SQLite.SQLiteConnection(this.DBPath))
    {
        List<Fruits> retrievedTasks = db.Table<Fruits>().ToList<Fruits>();
    }
}

.cs

public class Fruits
{
    public string ImgPath { get; set; }
    public string FruitName { get; set; }
    public string FruitColour{ get; set; }
}

I'm not really sure if the method i'm using is correct, please guide me. Thanks!

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
JustStarted
  • 199
  • 4
  • 13
  • Post your ListBox Xaml binding part! – Sajeetharan May 13 '14 at 13:55
  • I've provided two answers on questions that address a similar problem: [How can I bind data into Xaml, Windows Phone?](http://stackoverflow.com/questions/14153659/how-can-i-bind-data-into-xaml-windows-phone/14153702#14153702) or [How can I data bind a list of strings to a ListBox in WPF/WP7?](http://stackoverflow.com/questions/9391746/how-can-i-data-bind-a-list-of-strings-to-a-listbox-in-wpf-wp7/9391815#9391815) – Abbas May 13 '14 at 14:02
  • I've yet to do the bind cause I don't really have the idea how to start with. @Sajeetharan – JustStarted May 14 '14 at 03:08

1 Answers1

0

You have defined properties in your class, you have filled your List<Fruit> with items. What you should else do is (if you haven't done it already):

Define your List<Fruit> as ItemsSource of Listbox

public void ListFruit()
{
  using (var db = new SQLite.SQLiteConnection(this.DBPath))
  {
      List<Fruits> retrievedTasks = db.Table<Fruits>().ToList<Fruits>();
      myListbox.ItemsSource = retrievedTasks; // or directly
  }
} 

You may also think of using ObservableCollection instead of List - in this case you need to set ItemsSource only once - for example in constructor of the Page. You may also think of implementing INotifyPropertyChanged so that you can inform View when item changes.

And I'm not sure if you had defined Binding in your xaml (or somewhere else):

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
            <Image Source= "{Binding ImgPath}" Height="26" Width="50" Margin="12,10,9,0" VerticalAlignment="Top"/>
                <TextBlock Text="{Binding FruitName}"  FontSize="26" Style="{StaticResource BodyTextBlockStyle}"/>
                <TextBlock Text="{Binding FruitColour}"  FontSize="26" Style="{StaticResource BodyTextBlockStyle}"/>
        </StackPanel>
    </DataTemplate>
 </ListBox.ItemTemplate>
Romasz
  • 29,662
  • 13
  • 79
  • 154