c#UWP ListView与列绑定Sqlite

时间:2016-05-25 16:45:03

标签: c# xaml listview binding uwp

我正在尝试一个包含多个列的ListView,我的代码我得到了,但是我遇到了绑定代码的一些问题,因为我在两个列中都提供了相同的值。 我怎么能得到这个?

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将被删除。

1 个答案:

答案 0 :(得分:0)

  

我正在尝试一个包含多个列的ListView,我的代码我得到了,但是我遇到了绑定代码的一些问题,因为我在两个列中都提供了相同的值。我怎么能得到这个?

首先,创建一个名为Folders的新类:

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

然后,使用 List&lt; Folders&gt; 而不是List&lt; string&gt;设置ctlList的 ItemsSource

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;
}

最后,将Foldername和Path绑定到不同的列:

<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>

以下是Entire Sample供您参考。