Silverlight:如何将loadoperation的结果绑定到包含少量空值的LineSeries图表

时间:2011-11-03 05:12:00

标签: wpf silverlight c#-4.0 silverlight-toolkit

我已将我的图表绑定到加载操作的结果。加载操作返回的结果返回复杂类型,其中某些对象属性的值为null。

所以我的银光应用程序出错了。

我的XAML代码如下:

<Grid x:Name="LayoutRoot">
        <toolkit:Chart Name="historicalTotalChart" Title="Chart Title">
            <toolkit:LineSeries >

            </toolkit:LineSeries>
        </toolkit:Chart>
    </Grid>

我的CS文件中的代码如下:

LoadOperation<GetHistoricalTotalReport_Result> hisTotalsLoadOp =
                context.Load(context.GetHistoricalToalReportQuery(tableName, sD, startDate, endDate));

            LineSeries line = new LineSeries();
            line.ItemsSource = hisTotalsLoadOp.Entities;

            //line.DependentValuePath ="rep_date";
            //line.IndependentValuePath = "expr1";
            line.DependentValueBinding = new Binding("[rep_date]");
            line.IndependentValueBinding = new Binding("[expr1]");
            line.Title = "Historical Totals";
            historicalTotalChart.Series.Add(line);

任何人都可以说我怎么能克服这个错误?

rep_date,expr1是我的复杂类型GetHistoricalTotalReport_Result中的属性。我是否正确绑定了属性?

任何帮助都非常感激。

由于

1 个答案:

答案 0 :(得分:1)

您的申请中有3个可能的问题:

  1. 您的绑定不应包含方括号,它们适用于数组和字典。另外,我宁愿使用属性Dependent / IndependentValuePath,你不应该评论它们,它们是完全正确的。
  2. DependentValuePath属性必须是数值数据类型,因为它位于Y轴上。在您的示例中,rep_date属性可以是double类型。
  3. IndependentValuePath位于X轴上,可以是任何类型,但值不能包含空值。真的,我不知道在X轴上可以显示空值的位置,它没有意义。
  4. 以下是正确代码的示例:

    LineSeries line = new LineSeries();
    line.ItemsSource = new[]
    {
        new ItemViewModel{expr1 = "Item1", rep_date = 25},
        new ItemViewModel{expr1 = "Item2", rep_date = null},
        new ItemViewModel{expr1 = "Item3", rep_date = 31},
        new ItemViewModel{expr1 = "Item4", rep_date = null},
    };
    
    line.DependentValuePath = "rep_date";
    line.IndependentValuePath = "expr1";
    line.Title = "Historical Totals";
    historicalTotalChart.Series.Add(line);
    

    下一个代码不起作用:

    line.ItemsSource = new[]
    {
        new ItemViewModel{expr1 = null, rep_date = 25} //wrong, x-value can't be null
    }
    

    但您可以在显示之前过滤您的实体:

    line.ItemsSource = hisTotalsLoadOp.Entities.Where(e => e.expr1 != null).ToList();