Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm trying to use (for the first time) MVVM pattern to make a WPF chart work and I can't understand the problem! why I get nothing on my MainWindow while debugging ! in my output window i have this error message:

System.Windows.Data Error: 40 : BindingExpression path error: 'Data' property not found on >'object' ''String' (HashCode=-354185577)'. BindingExpression:Path=Data; DataItem='String' (HashCode=-354185577); target element is 'ColumnSeries' (Name=''); target property is >'ItemsSource' (type 'IEnumerable')

here is my mainwindow.xaml in myProject.View
HTML
<Window x:Class="Chart.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
     xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    Title="MainWindow" Height="350" Width="525"
    DataContext="Test">
<Grid>
    <chartingToolkit:Chart 
        Height="262" 
        HorizontalAlignment="Left" 
        Margin="33,0,0,620"
        Name="columnChart" 
        Title="ColumnSeriesDemo"
        VerticalAlignment="Bottom"
        Width="360">
              <chartingToolkit:ColumnSeries 
                  IndependentValueBinding="{Binding Path=DateTest, diag:PresentationTraceSources.TraceLevel=High}"
                  DependentValueBinding="{Binding Path=VolumeTest ,diag:PresentationTraceSources.TraceLevel=High}"
                  ItemsSource="{Binding Path=Data, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}" />
    </chartingToolkit:Chart>
</Grid>
</Window>


and here is my mainwindow.xaml

C#
namespace Chart
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private BindingVM Test;
    public MainWindow()
    {  
        this.Test = new BindingVM();   
        this.DataContext = Test;

        InitializeComponent();   
    }
}
}


here is my ModelView class in myProject.ModelView

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Chart.Model;
using System.Windows.Threading;
using System.ComponentModel;

namespace Chart.ViewModel
{
class BindingVM
{
    public BindingVM()
    {

       // AddElement();
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Start();

    }

    DataItem item = new DataItem();
    public DateTime DateTest
    {
        get { return item.date; }
        set { item.date = value;
              propChanged("date");
            }
    }

    public Double VolumeTest
    {
        get { return item.volume; }
        set
        {
            item.volume = value;
            propChanged("volume");
        }
    }
    public DispatcherTimer timer = new DispatcherTimer();

    public event PropertyChangedEventHandler PropertyChanged;
    public void propChanged(String propname)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propname));
        }
    }

   public ObservableCollection<DataItem> DataTest = new ObservableCollection<DataItem>();
   public ObservableCollection<DataItem> Data
   {
       get { return DataTest;}
       set { DataTest= value;
             propChanged("data");
           }
   }



   public void timer_Tick(object sender, EventArgs e)

    {
        Random rnd = new Random();
        double baseValue = 20 + rnd.NextDouble() * 10;
        double value = baseValue + rnd.NextDouble() * 6 - 3;

        DataTest.Add(new DataItem()
        {
            date = DateTime.Now,
            open = value + rnd.NextDouble() * 4 - 2,
            high = value + 2 + rnd.NextDouble() * 3,
            low = value - 2 - rnd.NextDouble() * 3,
            close = value + rnd.NextDouble() * 4 - 2,
            volume = rnd.NextDouble() * 200,

        });

        baseValue = value < 6 ? value + rnd.NextDouble() * 3 : value;



        // DataTest.RemoveAt(0);
    }
 }
}


here is my model in myProject.Model

<pre lang="c#">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chart.Model
{
public class DataItem
{
    public DateTime date { get; set; }
    public double open { get; set; }
    public double high { get; set; }
    public double low { get; set; }
    public double close { get; set; }
    public double volume { get; set; }
}

}


Any ideas why it doesn't work ???!!
Posted

1 solution

You set the DataContext of the MainWindow in xaml.

Remove this line in mainwindow.xaml:

DataContext="Test"

This should solve it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900