MVVM模式:BindingExpression路径错误:在“对象"上找不到“数据"属性 [英] MVVM Patttern: BindingExpression path error: 'Data' property not found on 'object'
问题描述
我正在尝试(第一次)使用MVVM模式使WPF图表工作,但我不明白该问题!为什么在调试时在MainWindow上什么也看不到!在我的输出窗口中,我收到以下错误消息:
System.Windows.Data错误:40:BindingExpression路径错误:在>''object''''''String''(HashCode = -354185577)''上找不到``Data''属性. BindingExpression:Path =数据; DataItem =''字符串''(HashCode = -354185577);目标元素是``ColumnSeries''(Name ='''');目标属性是>''ItemsSource''(类型``IEnumerable'')
这是myProject.View
中的mainwindow.xaml
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>
这是我的mainwindow.xaml
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();
}
}
}
这是myProject.ModelView中的ModelView类.
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);
}
}
}
这是我在myProject.Model
中的模型
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 ???!!
推荐答案
您可以在xaml中设置MainWindow
的DataContext
.
在mainwindow.xaml中删除此行:
DataContext="Test"
这应该可以解决.
You set theDataContext
of theMainWindow
in xaml.
Remove this line in mainwindow.xaml:
DataContext="Test"
This should solve it.
这篇关于MVVM模式:BindingExpression路径错误:在“对象"上找不到“数据"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!