WPF 绑定找不到数据项



我对数据绑定和MVVM的概念还很陌生,所以过去几天我一直在尝试。基本上,我想做的是使用LiveCharts库,我想从上传的CSV文件中同时显示多个图表。

到目前为止,我做得很好,但现在我正在编写代码,这样,如果我想增加一个图表中X轴的单位,我就可以与其他图表同时增加。(对于上下文,x轴是以时间戳为单位的,不同的图表应该具有相同的时间戳,但在相同时间戳下具有不同的ID和不同的值(

我的UI中的代码是这样的(省略了一些我认为不必要的代码(:

<ItemsControl ItemsSource="{Binding SeriesViews}" VerticalAlignment="Top">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="White">
<lvc:CartesianChart Series="{Binding}" Pan="X">
<lvc:CartesianChart.AxisX>
<lvc:Axis RangeChanged="Axis_RangeChanged" 
MinValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.Xmin}" 
MaxValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.Xmax}"
Separator="{x:Static lvc:DefaultAxes.CleanSeparator}">
</lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

另一端是这样的:

public GraphData gd;
public Graphs()
{
InitializeComponent();
}
public void GenerateCSVList(string csvPath)
{
using (var reader = new StreamReader(csvPath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
ExcelRecords = csv.GetRecords<Emotions>().ToList();
}
gd = new GraphData();
gd.Xmin = 0;
gd.Xmax = 5;
gd.SeriesViews = ReturnChart();
this.DataContext = gd;
}
public ObservableCollection<SeriesCollection> ReturnChart()
{
ObservableCollection<SeriesCollection> ChartCollection = new ObservableCollection<SeriesCollection>();
//Draw charts here
return ChartCollection
}
public class GraphData : INotifyPropertyChanged
{
public ObservableCollection<SeriesCollection> SeriesViews { get; set; }
private double _xmin;
private double _xmax;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public double Xmin
{
get { return _xmin; }
set
{
_xmin = value;
OnPropertyChanged("Xmin");
}
}
public double Xmax
{
get { return _xmax; }
set
{
_xmax = value;
OnPropertyChanged("Xmax");
}
}
}

不过,我目前在最小值和最大值方面都出现了这个错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Grid', AncestorLevel='1''. BindingExpression:Path=DataContext.Xmax; DataItem=null; target element is 'Axis' (Name=''); target property is 'MaxValue' (type 'Double')

我希望能朝着正确的方向前进,因为这一切对我来说都是全新的。我在装订时错过了什么吗?我想通过把它们放在一个类中,我可以从UI访问它们。为什么我可以查看SeriesViews,但不能查看同一类的Xmin和Xmax?

问题是,您没有将Grid作为ItemsControl的父容器。将AncestorType更改为ItemsControl,将使绑定生效。

<lvc:Axis RangeChanged="Axis_RangeChanged" 
MinValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.Xmin}" 
MaxValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.Xmax}"
Separator="{x:Static lvc:DefaultAxes.CleanSeparator}">
</lvc:Axis>

想明白了。以下是工作代码:

<lvc:Axis RangeChanged="Axis_RangeChanged" DisableAnimations="True" Position="LeftBottom" 
MinValue="{Binding DataContext.Xmin, Source={x:Reference Root}}" 
MaxValue="{Binding DataContext.Xmax, Source={x:Reference Root}}" 
Separator="{x:Static lvc:DefaultAxes.CleanSeparator}">

我将UserControl命名为根

最新更新