代码之家  ›  专栏  ›  技术社区  ›  Mads Mobæk

将字典绑定到图表

  •  1
  • Mads Mobæk  · 技术社区  · 15 年前

    我是Silverlight的新手,我尝试在图形中显示字典的内容:

    在代码后面:

    ChartData = new Dictionary<DateTime, double> {{DateTime.Now, 10}, 
          {DateTime.Now, 20}, {DateTime.Now, 15}};
    

    在Silverlight XAML中:

    <toolkit:Chart HorizontalAlignment="Left" Margin="113,168,0,0" Name="chart1" 
       Title="Chart Title" VerticalAlignment="Top">
        <toolkit:LineSeries ItemsSource="{Binding Path=ChartData}"  
              DependentValuePath="Key" IndependentValuePath="Value">
        </toolkit:LineSeries>
    </toolkit:Chart>
    

    但这给出了“没有合适的轴可用于绘制相关值”。建议?

    1 回复  |  直到 15 年前
        1
  •  2
  •   AnthonyWJones    15 年前

    尝试此数据集:

    ChartData = new Dictionary<DateTime, double>() { 
      { DateTime.Now.AddDays(-1), 10 }, 
      { DateTime.Now, 20 },
      { DateTime.Now.AddDays(1), 15 }
    };
    

    (我很惊讶你的代码行竟然成功了,因为它会试图在字典中添加多个具有相同值的键)。

    然后更改您的XAML:

    <toolkit:LineSeries ItemsSource="{Binding Path=ChartData}"    
          DependentValuePath="Value" IndependentValuePath="Key">
    

    将日期用作DependentValue是非常不常见的,事实上,我无法想到DependentValue会是除数字以外的任何其他值。