1

I am trying a ListView with multiple columns, with my code I get that, but i got some problem with the binding code, because I am geting the same values in both columns. How I can get this?

MainPage.xaml

    <Page
    x:Class="SQlite_Test_01.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SQlite_Test_01"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="btnCreateDB" Content="Crear DataBase" HorizontalAlignment="Left" Margin="23,10,0,0" VerticalAlignment="Top" Height="83" Width="255" Click="btnCreateDB_Click"/>
        <Button x:Name="btnFillList" Content="Llenar Lista 1" HorizontalAlignment="Left" Margin="295,10,0,0" VerticalAlignment="Top" Height="83" Width="299" Click="btnFillList_Click"/>

        <ListView x:Name="ctlList" Margin="23,113,0,241" BorderBrush="Black" BorderThickness="1" Background="White" HorizontalAlignment="Left" Width="944" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding ElementName=ctlList , Path=ActualWidth }" Padding="0" Margin="0" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150" />
                            <ColumnDefinition Width="300" />
                            <ColumnDefinition Width="000" />
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="Col1" Text="{Binding}" Grid.Column="0" TextWrapping="Wrap" />
                        <TextBox x:Name="Col2" Text="{Binding}" Grid.Column="1" TextWrapping="Wrap" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <ListBox x:Name="ctlList_2" Margin="1047,113,0,241" BorderBrush="Black" BorderThickness="1" Background="White" HorizontalAlignment="Left" Width="155" ScrollViewer.IsVerticalScrollChainingEnabled="True"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding ActualWidth, ElementName=ctlList_2}" Padding="0" Margin="0" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200" />
                            <ColumnDefinition Width="0" />
                            <ColumnDefinition Width="0" />
                        </Grid.ColumnDefinitions>
                        <TextBox Text="{Binding}" Grid.Column="0" TextWrapping="Wrap" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Page>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using SQLitePCL;




// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace SQlite_Test_01
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        public SQLitePCL.SQLiteConnection dbConnection = new SQLiteConnection("folders.db");


        private void btnCreateDB_Click(object sender, RoutedEventArgs e)
        {
            SQLiteConnection dbConnection = new SQLiteConnection("folders.db");
            string sSQL = @"CREATE TABLE IF NOT EXISTS Folders
                                (IDFolder Integer Primary Key Autoincrement NOT NULL
                                , Foldername VARCHAR ( 200 )
                                , Path VARCHAR ( 255 ));";
            ISQLiteStatement cnStatement = dbConnection.Prepare(sSQL);
            cnStatement.Step();
        }


        private void btnFillList_Click(object sender, RoutedEventArgs e)
        {
            var items1 = new List<String>();
            var items2 = new List<String>();
            string sSQL = @"SELECT [Foldername],[Path] FROM Folders";
            ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
            while (dbState.Step() == SQLiteResult.ROW)
            {
                string sFoldername = dbState["Foldername"] as string;
                string sPath = dbState["Path"] as string;
                items1.Add(sFoldername);
                items2.Add(sPath);
                ;
            }
            ctlList.ItemsSource = items1;
            ctlList_2.ItemsSource = items2;

        }




    }
}

PD: ctlList_2 will be erased when I resolve my trouble.

1 Answer 1

1

I am trying a ListView with multiple columns, with my code I get that, but i got some problem with the binding code, because I am geting the same values in both columns. How I can get this?

First, create a new class called Folders:

public class Folders
{
    public string Foldername { get; set; }
    public string Path { get; set; }
}

Then, set the ItemsSource for ctlList with List<Folders> instead of List<string>:

private void btnFillList_Click(object sender, RoutedEventArgs e)
{
    // set the ItemsSource for ctlList with List<Folders> instead of List<string>:
    var items1 = new List<Folders>();

    string sSQL = @"SELECT [Foldername],[Path] FROM Folders";
    ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
    while (dbState.Step() == SQLiteResult.ROW)
    {
        string sFoldername = dbState["Foldername"] as string;
        string sPath = dbState["Path"] as string;
        Folders folder = new Folders() { Foldername = sFoldername, Path = sPath };
        items1.Add(folder);
        ;
    }
    ctlList.ItemsSource = items1;
}

Last, bind Foldername and Path to different columns:

<DataTemplate>
    <Grid Width="{Binding ElementName=ctlList , Path=ActualWidth }" Padding="0" Margin="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="300" />
            <ColumnDefinition Width="000" />
        </Grid.ColumnDefinitions>
        <TextBox x:Name="Col1" Text="{Binding Path=Foldername}" Grid.Column="0" TextWrapping="Wrap" />
        <TextBox x:Name="Col2" Text="{Binding Path=Path}" Grid.Column="1" TextWrapping="Wrap" />
    </Grid>
</DataTemplate>

Here is Entire Sample for your reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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