MVVM Patttern: BindingExpression path error: property "Data" not found on "object"
I'm trying to use (for the first time) the MVVM pattern to make a WPF diagram and I can't figure out the problem! why am I not getting anything from 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 in> 'object' 'String' (HashCode = -354185577) '. BindingExpression: Path = Data; DataItem = 'String' (HashCode = -354185577); target element - "ColumnSeries" (Name = ''); target property> '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 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 would be really appreciated: '(
source to share
I know this answer is very late, but I'm sure it will help someone out there.
I spent hours because I ran into this error, which usually happens to newbies like me:
the error says:
BindingExpression path error: property not found on 'object'
and it's a really clear message if I'm focused from the beginning.
in my case i found that i defined a feild with no property
as shown below:
public string Name = "textss";
and I, a XAML processor, want the property to be fully implemented like this:
public string _name = "textss";
public string Name{
get{
return _name;
}
set{
_name = value ;
}
}
or at least as follows
public string Name {get; set;}
so the answer to this question here is that the Date property has some kind of bug! I think the mistake is calling propChanged and passing the name with lowercase "data" and not "Data"
public ObservableCollection<DataItem> DataTest = new ObservableCollection<DataItem>();
public ObservableCollection<DataItem> Data
{
get { return DataTest;}
set { DataTest= value;
propChanged("data");
}
}
source to share