1

我正在尝试将我的数据映射到 ViewModel 中的图表。我设置绑定,填充对象,但由于某种原因没有显示数据。您能否验证我做错了什么?谢谢!

查看模型文件:

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

1 回答 1

0

如果您不关心数据更新,如果您只是显示来自其他地方的数据,我建议您将 datacontext 初始化移动到构造函数中,这将在视图准备好之后发生,并且它将检索您的组而无需必须实现 INotifyPropertyChanged。即使集合需要更改,这也很有可能,因为一旦设置绑定,集合就会触发 CollectionChanged 事件。

于 2013-02-24T17:18:36.650 回答