1

我正在开发 Windows 商店应用程序。我正在使用 sqlite 数据库,我想将 select 查询的结果放入 listview。我的列表视图有网格,这些网格内有文本框。但是,当我给它们命名时,我无法在 c# 中定位这些文本框。这是项目中的代码示例:第一个 sqlite 查询:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    string DBPath = string.Empty;
    DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "mydb.s3db");

    using (var db= new SQLite.SQLiteConnection(DBPath))
    {
        var query= db.Table<company>();
        var result= query.ToList();

        foreach( var x in result )
        {
            company frm= new company();
            //addition to listview...
        }
    }

}

除了输入列表视图之外,一切都在上面完美运行。我已经用列表视图之外的文本块对其进行了测试。

和 xml:

<ListView Name="lv" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,10,10" Grid.Row="3" Grid.RowSpan="11" Grid.ColumnSpan="8">
    <ListView.HeaderTemplate>
        <DataTemplate>
            <Grid Loaded="gHeader_Loaded">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="2" Grid.Column="0" Text="company no" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Text="company name" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="2" Text="phone" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="3" Text="fax" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="4" Text="address" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="5" Text="state" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="6" Text="city" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <ComboBox Grid.Row="2" Grid.Column="7"  HorizontalAlignment="Center" VerticalAlignment="Center">
                    <ComboBoxItem Content="test1"></ComboBoxItem>
                    <ComboBoxItem Content="test2"></ComboBoxItem>
                    <ComboBoxItem Content="test3"></ComboBoxItem>
                    <ComboBoxItem Content="test4"></ComboBoxItem>
                    <ComboBoxItem Content="test5"></ComboBoxItem>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </ListView.HeaderTemplate>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Loaded="gHeader_Loaded">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock x:Name="test" Grid.Row="2" Grid.Column="0" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="2" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="3" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="4" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="5" Text="" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

很抱歉,这篇文章很长,我想提供有关该程序的详细信息,然后也许您可以看到出了什么问题。我将不胜感激任何帮助/反馈。

致以我的问候...

4

1 回答 1

1

您需要对 ListView 使用数据绑定。

首先将您的集合绑定到 ListView,如下所示:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
   ** snip **    
    using (var db= new SQLite.SQLiteConnection(DBPath))
    {
        var query= db.Table<company>();
        lv.ItemsSource = query.ToList();
    }
}

之后,您可以将数据模板内部绑定到对象的属性。假设您的公司类定义如下:

public class Company
{
    public string Name{get; set;}
}

然后您可以在 ItemTemplate 中执行此操作:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid Loaded="gHeader_Loaded">
            <Grid.ColumnDefinitions>
               ** snip **
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
               ** snip **
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding Name}" />
            ** snip **
        </Grid>
     </DataTemplate>
</ListView.ItemTemplate>

渲染每个项目时,它会查看当前实例(类型为company),提取Name-property 并将其作为TextBlock.

于 2013-05-12T23:06:21.197 回答