返回首页

可以somone请帮我解决它把我逼疯!|嗨,这已经把我逼疯了几天现在我已经张贴在这里,但所有的建议似乎没有工作至今。下面是我的C#代码的XAML部分 - 如果有不再需要我将张贴任何在必要!

我明白提供任何帮助!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Data.SqlClient;

using System.Data;

using System.Collections.ObjectModel;

 

namespace WpfApplication4

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            _DateLabel.Content = DateTime.Today;

        }

       

        private double _height, _weight, _target;

        ObservableCollection<fooddata> _FoodDataCollection = new ObservableCollection<fooddata>();

      

        public ObservableCollection<fooddata> FoodDataCollection

        { get { return _FoodDataCollection; } }

 

        public class FoodData

        {

            public string Food_Name { get; set; }

            public string Calories { get; set; }

            public string Fat { get; set; }

            public string Protein { get; set; }

            public string Carbs { get; set; }

        }  

 

        private void calendar1_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)

        {

            _DateLabel.Content = calendar1.SelectedDate;

        }

 

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            SqlConnection conn = new SqlConnection(WpfApplication4.Properties.Settings.Default.CalorieCounterConnectionString);

            conn.Open();

 

            using (SqlCommand command = new SqlCommand("SELECT TOP 1 User_Name,User_Height,User_Weight,User_Target,User_Target_Date FROM [User]", conn))

            {

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())

                {

                    _userName.Content = reader.GetString(0);

                    _height = reader.GetDouble(1);

                    _weight = reader.GetDouble(2);

                    _target = reader.GetDouble(3);

                    _targetDate.Content = reader.GetDateTime(4);

                }

            }

            _heightLbl.Content = _height;

            _weightLbl.Content = _weight;

            _targetWeight.Content = _target;

            

            _toLose.Content = (_weight - _target);

            _caloriesLeft.Content = ((_weight - _target) * 3500);

            conn.Close();

 

              using (DataClasses1DataContext dc = new DataClasses1DataContext())

                 {

                     listView1.ItemsSource = dc.Foods.ToList();

                 }

 

        }

            

        

 

        private void button6_Click(object sender, RoutedEventArgs e)

        {

            AddFood addfood = new AddFood();

            addfood.Show();

            addfood.Focus();

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            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();

                

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

 

                    });

                }

                

 

            }

        }

  

    }

}</fooddata></fooddata></fooddata>

XAML:

{C}
 <listview height="209" horizontalalignment="Left" margin="526,321,0,0" name="listView1" selectionmode="Single" verticalalignment="Top" width="304" itemssource="{Binding FoodDataCollection}">

            <listview.view>

                <gridview>

                    <gridviewcolumn header="Name" width="120" displaymemberbinding="{Binding Path= Food_Name}" />

                    <gridviewcolumn header="Calories" width="62" displaymemberbinding="{Binding Path= Calories}" />

                    <gridviewcolumn header="Fat" width="30" displaymemberbinding="{Binding Path=Fat}" />

                    <gridviewcolumn header="Protein" width="50" displaymemberbinding="{Binding Path= Protein}" />

                    <gridviewcolumn header="Carbs" width="40" displaymemberbinding="{Binding Path= Carbs}" />

                </gridview>

            </listview.view>

        </listview>

我认为这是所需要的所有位!

我从这个没有错误,但在列表视图,而不是它返回的数据 - WpfApplication4.Food - 在每一列appers,所以我知道这是阅读和丁东西,但实体商店没有什么,我想它{BR! }
事先得到安宁的任何帮助的感谢!

担| DanHodgson88:米卡Wendelius:尝试定义为您gridviewcolumns]

回答