gpt4 book ai didi

wpf - MVVM 模式 : BindingExpression path error: 'Data' property not found on 'object'

转载 作者:行者123 更新时间:2023-12-04 15:56:29 24 4
gpt4 key购买 nike

我正在尝试(第一次)使用 MVVM 模式来使 WPF 图表工作,但我无法理解这个问题!为什么在调试时我的 MainWindow 上什么都没有!在我的输出窗口中我有这个错误信息:

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')

这是我在 myProject.View 中的 mainWindow.xaml

    <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>

这是我的 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();
}
}
}

这是我在 MyProject.ModelView 中的 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 中的模型

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

}

任何帮助将不胜感激:'(

最佳答案

删除行

DataContext="Test"

从 XAML 的顶部开始。

您在代码中设置 DataContext,但随后在 XAML 中再次将其设置为字符串。

关于wpf - MVVM 模式 : BindingExpression path error: 'Data' property not found on 'object' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9840249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com