gpt4 book ai didi

c# - Windows Store App 将 sqlite 查询写入 ListView 的文本 block

转载 作者:行者123 更新时间:2023-12-03 19:51:10 25 4
gpt4 key购买 nike

我正在开发 Windows 商店应用程序。我正在使用 sqlite 数据库,我想将 select 查询的结果放入 listview。我的 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...
}
}

}

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

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

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

致以我的问候...

最佳答案

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

首先将您的集合绑定(bind)到 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();
}
}

之后,您可以将数据模板内部绑定(bind)到对象的属性。假设您的公司类定义如下:
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 的值.

关于c# - Windows Store App 将 sqlite 查询写入 ListView 的文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com