在Windows 8应用程序上工作,我有来自sqlite表的列表,并希望将此列表与combobox绑定。我的xaml看起来像这样。
<ListView Grid.Row="1" x:Name="CityListView" Height="180">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="1" BorderThickness="0" Background="#7CBF42" CornerRadius="6">
<StackPanel Orientation="Horizontal" Width="auto" Height="auto">
<ComboBox ItemsSource="{Binding CityList}" DisplayMemberPath="Name" SelectedValuePath="Name"/>
</StackPanel>
</Border>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在.cs文件中,我从sqlite db table city_list中获取城市列表,其中包含两个字段名称和代码。
public ObservableCollection<city_list> CityList { get; private set; }
public CityListClass()
{
this.InitializeComponent();
CityList = new ObservableCollection<city_list>();
CityListView.DataContext = this;
// Adds a listview item
CityListView.Items.Add(1);
getCityListFormDB();
}
private async void getCityListFormDB()
{
try
{
var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "city_db.sqlite");
using (var conn = new SQLite.SQLiteConnection(dbpath))
{
var result = from x in conn.Table<city_list>() select x;
foreach (var item in result)
{
CityList.Add(item);
}
conn.Dispose();
conn.Close();
}
}
catch(SQLiteException EX)
{
}
}
public class city_list
{
public string Name{ get; set; }
public int code{ get; set; }
}
我无法理解为什么combobox没有显示任何列表。请帮助。
答案 0 :(得分:0)
尝试在您的ComboBox中添加DataTemplate。
<StackPanel Orientation="Horizontal" Width="auto" Height="auto">
<ComboBox ItemsSource="{Binding CityList}" DisplayMemberPath="Name" SelectedValuePath="Name">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
您还应该检查以确保getCityListFormDB
正在返回行。