简体   繁体   English

在Windows 8应用程序中将数据绑定到Listview内部的组合框

[英]Data Binding to Combobox inside Listview in windows 8 app

Working on windows 8 application , i have list from sqlite table and want to bind this list with combobox. 在Windows 8应用程序上工作,我从sqlite表中获取列表,并希望将此列表与combobox绑定在一起。 My xaml looks like this. 我的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>

And in .cs file, I fetch the city list from sqlite db table city_list with Two fields Name and code. 在.cs文件中,我从带有两个字段Name和code的sqlite db表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; }
    }

I am unable to understand why combobox does not shows any list.Please help. 我无法理解为什么combobox不显示任何列表。请帮助。

Try adding a DataTemplate to your ComboBox. 尝试将DataTemplate添加到您的ComboBox。

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

You should also check to make sure that getCityListFormDB is returning rows. 您还应该检查以确保getCityListFormDB返回行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM