可以请你帮我解决这个问题,让我发疯! [英] Could somone please help me solve this its driving me mad!

查看:62
本文介绍了可以请你帮我解决这个问题,让我发疯!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经让我发疯了几天了,我已经在这里发布了,但是到目前为止,所有建议似乎都没有奏效.以下是我的C#代码以及XAML的一部分-如果有需要,我将发布所有需要的内容!

感谢提供的任何帮助!

Hi, this has been driving me mad for a few days now and I have posted on here but all the suggestions haven''t seem to work so far. Below is my C# code and also parts of the XAML - if there is anymore needed I will post whatever is neeeded!!

I appreciate any help that is offered!

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:




XAML :

<window x:class="WpfApplication4.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="925" Width="1075"  WindowStartupLocation="CenterScreen" Loaded="Window_Loaded"></window>





<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-此属性适用于每一列,因此我知道它正在读取和读取内容,但绝对不是我所读的内容想要它!

在此先感谢您的帮助!

Dan



I think that is all the bits that are needed!

What I am getting back from this is no error but in the list view instead of the data it''s returning - WpfApplication4.Food - this appers in each column so I know it''s reading and ding something but definetly not what I want it to!

Thanks in advance for any help givin!!

Dan

推荐答案

尝试定义 ^ ].


这篇关于可以请你帮我解决这个问题,让我发疯!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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