【问题标题】:Trying to build a chart in C# wpf ,binding not happening in c# wpf试图在 C# wpf 中构建图表,但在 c# wpf 中没有发生绑定
【发布时间】:2016-06-11 13:24:11
【问题描述】:

我正在尝试在 wpf c# 中构建柱形图,并且我正在使用 DataVisuializationTool Kit。我按照此网络链接中的程序进行操作,并按照它所说的做了。我有数据,但最终绑定没有发生。 这是我关注的链接::http://www.c-sharpcorner.com/uploadfile/mahesh/column-chart-in-wpf/

   Here is my code.::
    ........................................................................


                        if (DG1_Liters > 0)
                        {
                            DG1_Liters = (DG1_Liters / DG_NUM_RECORDS_IN_THAT_HOUR) + ((DG1_C * DG_NUM_RECORDS_IN_THAT_HOUR) / 60);

                            dbCollection.Add(new VM.Chart.AxisData((startTime24), (Convert.ToInt32(DG1_Liters)), 0, 0));
                            shellVM.chartVM.MinimumVal = 0;
                            shellVM.chartVM.MaximumVal = 500;

                        }
                        if (DG2_Liters > 0)
                        {

                            DG2_Liters = (DG3_Liters / DG_NUM_RECORDS_IN_THAT_HOUR) + ((DG2_C * DG_NUM_RECORDS_IN_THAT_HOUR) / 60);
                            dbCollection.Add(new VM.Chart.AxisData((startTime24),0, (Convert.ToInt32(DG2_Liters)), 0));
                            shellVM.chartVM.MinimumVal = 0;
                            shellVM.chartVM.MaximumVal = 500;

                        }
                        if (DG3_Liters > 0)
                        {

                           DG3_Liters = (DG3_Liters / DG_NUM_RECORDS_IN_THAT_HOUR) + ((DG3_C * DG_NUM_RECORDS_IN_THAT_HOUR) / 60);
                            // dbCollection.Add(new VM.Chart.AxisData((startTime24),Convert.ToInt32(DG3_Liters)));
                            dbCollection.Add(new VM.Chart.AxisData((startTime24),0, 0 ,(Convert.ToInt32(DG3_Liters))));
                            shellVM.chartVM.MinimumVal = 0;
                            shellVM.chartVM.MaximumVal = 500;

                        }
                  }

               }

        }

 /*comment:: I am getting/setting some sets of data from AXIS clas property and storing it in a collection in DG3_Liters,DG1_Liters,DG2_Liters . 
       and finally creating the chart below.*/

    this.shellVM.chartVM.LineGraphTitle = "X Axis - Time \n\nY Axis - " + YAxis;  

    ((ColumnSeries)this.chart.chart.Series[0]).ItemsSource = dbCollection;
    ((ColumnSeries)this.chart.chart.Series[1]).ItemsSource = dbCollection;
    ((ColumnSeries)this.chart.chart.Series[2]).ItemsSource = dbCollection;

这是我的图表代码

     ..............Chart.Xml...........
   d:DesignHeight="300" d:DesignWidth="300"
                xmlns:dv="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
                 xmlns:dvc="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
                 xmlns:local="clr-namespace:ILS.VM.Chart;assembly=ILS.VM"
                 >
    <Grid>
        <dvc:Chart Name="chart">
            <dvc:Chart.Series>
                <dvc:ColumnSeries Title="DG 1"

                                  IndependentValueBinding="{Binding Path=Time}"
                                  DependentValueBinding="{Binding Path=DG1}" />
                <dvc:ColumnSeries Title="DG 2"

                                  IndependentValueBinding="{Binding Path=Time}"
                                  DependentValueBinding="{Binding Path=DG2}" />
                <dvc:ColumnSeries Title="DG 3"

                                  IndependentValueBinding="{Binding Path=Time}"
                                  DependentValueBinding="{Binding Path=DG3}" />

            </dvc:Chart.Series>
        </dvc:Chart>

这是我的轴类代码属性

public class AxisData
    {
        public AxisData(DateTime time, int DG1,int DG2,int DG3)
        {
            this.Time = time;
            this.DG1 = DG1;
            this.DG2 = DG2;
            this.DG3= DG3;
        }
        public DateTime Time
        {
            get;
            set;
        }
        public int DG1
        {
            get;
            set;
        }
        public int DG2
        {
            get;
            set;
        }
        public int DG3
        {
            get;
            set;
        }
    }

我以这种形式获取数据:: DG1=6 DG2=3 DG3=7 Time =system.time。等等收藏。我把它放在图表项目源中,但没有发生我所缺少的绑定。图表是否需要另一个获取/设置属性,如果需要,我该如何连接它。帮帮我。

【问题讨论】:

    标签: c# wpf visual-studio data-binding charts


    【解决方案1】:

    您的类 AxisData 没有实现 INotifyPropertyChanged,请参阅 MSDN

    实现可能如下所示:

    public class ObservableClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
        private int _dG1;
        public int DG1
        {
            get { return _dG1; }
            set
            {
                _dG1 = value;
                NotifyPropertyChanged("DG1");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 2014-09-26
      • 2016-02-24
      相关资源
      最近更新 更多