0

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you dude, you're life saver. I've spent about a week on this and couldn't find any explanation how to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.