3
votes

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>

and here is the my mainWindow.cs

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

and here is my model in myProject.Model

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 help will be appreciated really :'(

2

2 Answers

5
votes

Remove the line

DataContext="Test"

from the top of your XAML.

You are setting the DataContext in your code, but then setting it again to a string in your XAML.

2
votes

I know this reply is extremely late, but i am sure it will help someone there.

i have spent hours because i faced this error that happen usually with beginners like me :

the error says:

BindingExpression path error: property not found on 'object'

and it is really clear message if I concentrated since the begining .

in my case I found that i defined the feild without the property

like the following:

public string Name = "textss";

and I the XAML processor want a property fully implemented like this:

public string _name = "textss";

public string Name{
get{
    return _name;
    }
set{ 
    _name = value ;
    }
}

or at least as the following

public string Name {get; set;} 

so the answer to this question here is that the Date Property has some error !! I think the error is in calling propChanged and passing the name with small letter "data" not "Data"

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