简体   繁体   中英

Binding data from SQLite to ListView in Windows Phone 8.1

First I've tried other samples answered here on stackoverflow and tried other examples too but did not find success in my case.

I'm trying to bind data from a table in DB to listview ListView itself looks like this:

<ListView x:Name="lbItems" Width="Auto" Height="Auto" Margin="10" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Opacity="100" BorderThickness="0" Foreground="White" Background="Transparent">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="60" />
            </Grid.ColumnDefinitions>
            <CheckBox Grid.Column="0" x:Name="cbx" IsChecked="{Binding IsChecked}" Content="{Binding Content}" Height="58" Margin="10, 0, 0, 0" HorizontalAlignment="Stretch" VerticalAlignment="Center" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" Width="Auto" />
            <AppBarButton Tag="{Binding Id}" Grid.Column="1" Icon="Delete" Height="58" HorizontalAlignment="Right" Width="60" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" Margin="0,0,10,0" Click="DeleteButton_Click" />
        </Grid>
    </ListView>

and then in C# code behind:

public sealed partial class MainPage : Page
{
    ObservableCollection<tbl_Items> DB_ItemsList = new ObservableCollection<tbl_Items>();

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        ReadAllItemsList dbitems = new ReadAllItemsList();
        DB_ItemsList = dbitems.GetAllItems();
        lbItems.ItemsSource = DB_ItemsList.OrderBy(i => i.Id).ToList();
    }

Here I am following this example for creating the database and database operations: LINK

As a result in my listview I am getting only my table name (instead of binding the content from that table). I can't figure out what I am doing wrong. pls help.

Keep the template code of Listview inside DataTemplate of ListView like this.

            <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="60" />
                    </Grid.ColumnDefinitions>
                    <CheckBox Grid.Column="0" x:Name="cbx" IsChecked="{Binding IsChecked}" Content="{Binding Content}" Height="58" Margin="10, 0, 0, 0" HorizontalAlignment="Stretch" VerticalAlignment="Center" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" Width="Auto" />
                    <AppBarButton Tag="{Binding Id}" Grid.Column="1" Icon="Delete" Height="58" HorizontalAlignment="Right" Width="60" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" Margin="0,0,10,0" Click="DeleteButton_Click" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM