列表视图填充麻烦 [英] List view populate trouble

查看:70
本文介绍了列表视图填充麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我无法从下面的数据库填充列表视图是我的代码,到目前为止,所有更改都将不胜感激!

Hi all I am having trouble populating a listview from a database below is my code so far any alterations would be appreciated!

DataSet dtSet = new DataSet();
         using (SqlConnection conn = new SqlConnection(WpfApplication4.Properties.Settings.Default.CalorieCounterConnectionString))
         {
             conn.Open();
             SqlCommand command = new SqlCommand("SELECT Food_Name,Food_Calories,Food_Fat,Food_Protein,Food_Carbs FROM [Food]", conn);
             SqlDataReader dr = command.ExecuteReader();
             listView1.Items.Clear();
             while (dr.Read())
             {

                 _FoodDataCollection.Add(new FoodData
                 {
                     Food_Name = dr["Food_Name"].ToString(),
                     Calories = dr["Food_Calories"].ToString(),
                     Fat = dr["Food_Fat"].ToString(),
                     Protein = dr["Food_Fat"].ToString(),
                     Carbs = dr["Food_Carbs"].ToString()

                 });
             }


         }



XAML:



XAML:

DataContext="{Binding RelativeSource={RelativeSource Self}}"







<listview height="209" horizontalalignment="Left" margin="197,321,0,0" name="listView2" verticalalignment="Top" width="304" selectionchanged="listView2_SelectionChanged" selectionmode="Single">
            <listview.view>
                <gridview>
                    <gridviewcolumn header="Name" width="120" displaymemberbinding="{Binding Food_Name}" />
                    <gridviewcolumn header="Calories" width="62" displaymemberbinding="{Binding Calories}" />
                    <gridviewcolumn header="Fat" width="30" displaymemberbinding="{Binding Fat}" />
                    <gridviewcolumn header="Protein" width="50" displaymemberbinding="{Binding Protein}" />
                    <gridviewcolumn header="Carbs" width="40" displaymemberbinding="{Binding Carbs}" />
                </gridview>
            </listview.view>
        </listview>





I have no idea what the problem is help would be appreciated!

推荐答案

1)您声明您的DataContext已绑定,但没有绑定目标元素.

2)在您的DataContext绑定中,您编写RelativeSource={RelativeSource Self},这意味着源是要为其设置绑定的DataContext的元素,我认为这不是您真正想要的,但是如果您对xaml和代码,这很难说.

3)您没有在DataContext的绑定中指定路径,因此DataContext设置为您要在其DataContext上设置绑定的元素的实例.
4)您未在提供的XAML或代码中设置ListView的ItemsSource.
1) You state that your DataContext is binded but not which element that is the target element.

2) In your DataContext binding you write RelativeSource={RelativeSource Self} this mean the source is the element whose DataContext you''re setting a binding on, I don''t think that is what you really want, however without more insight into your xaml and code it''s impossible to tell.

3) You don''t specify a path in the binding for the DataContext, so the DataContext is set to the instance of the element whose DataContext you''re setting the binding on

4) You do not set the ItemsSource of the ListView in the provided XAML or code


这篇关于列表视图填充麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆