Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF MVVM Chart Binding
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
 
<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
 
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
 
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 23-Mar-12 5:25am
ftita359

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You set the DataContext of the MainWindow in xaml.
 
Remove this line in mainwindow.xaml:
 
DataContext="Test"
 
This should solve it.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 18 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100