我正在尝试将数据映射到ViewModel的图表中。我设置了绑定(bind),并填充了对象,但由于某种原因,没有数据显示。您能验证我做错了吗?谢谢!
查看模型文件:
QueryableCollectionView qcv;
public QueryableCollectionView Groups
{
get
{
return qcv;
}
set
{
qcv = value;
}
}
public RevenueChartViewModel()
{
List<Revenue> list = new List<Revenue>
{
new Revenue{Rev = 10, Category = 1, Date = DateTime.Today.AddDays(1)},
new Revenue{Rev = 20, Category = 1, Date = DateTime.Today.AddDays(3)},
new Revenue{Rev = 30, Category = 2, Date = DateTime.Today.AddDays(6)},
};
qcv = new QueryableCollectionView(list);
qcv.GroupDescriptors.Add(new GroupDescriptor() { Member = "Category" });
}
XAML:
<UserControl x:Class="FrontEnd.RevenueChart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FrontEnd"
mc:Ignorable="d" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" HorizontalAlignment="Stretch"
xmlns:telerikChart="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Charting"
xmlns:telerikCharting="clr-namespace:Telerik.Windows.Controls.Charting;assembly=Telerik.Windows.Controls.Charting"
xmlns:telerikControls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls">
<UserControl.DataContext>
<local:RevenueChartViewModel/>
</UserControl.DataContext>
<Grid>
<telerikChart:RadChart x:Name="radChart1">
<telerik:Series DataSeries="{Binding Groups}">
</telerik:Series>
<telerik:RadChart.SeriesMappings>
<telerikCharting:SeriesMapping LegendLabel="Product Sales">
<telerikCharting:SeriesMapping.SeriesDefinition>
<telerikCharting:AreaSeriesDefinition/>
</telerikCharting:SeriesMapping.SeriesDefinition>
<telerikCharting:SeriesMapping.ItemMappings>
<telerikCharting:ItemMapping DataPointMember="XCategory" FieldName="Date"/>
<telerikCharting:ItemMapping DataPointMember="YValue" FieldName="Rev"/>
</telerikCharting:SeriesMapping.ItemMappings>
</telerikCharting:SeriesMapping>
</telerik:RadChart.SeriesMappings>
</telerikChart:RadChart>
</Grid>
</UserControl>
最佳答案
如果您不关心数据更新,那么在您只是从其他地方显示数据的情况下,我建议您只是将datacontext初始化移到构造函数中,这将在 View 就绪后发生,并且它将检索您的组而不会必须实现INotifyPropertyChanged。即使集合需要更改,也很有可能这样做很好,因为设置绑定(bind)后,集合将触发CollectionChanged事件。
关于c# - MVVM绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15053235/