MVVM绑定不起作用

时间:2013-02-24 15:26:54

标签: c# wpf mvvm telerik

我正在尝试将数据映射到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>

1 个答案:

答案 0 :(得分:0)

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