1

我有一个包含不同项目的组合框。根据此组合框的值,我希望在图表中显示该项目上已注册的不同时间。

我创建的图表的代码:

                    <chartingToolkit:Chart Height="262" HorizontalAlignment="Left" 
                    Margin="33,0,0,620" Name="columnChart"
                    VerticalAlignment="Bottom" Width="360" BorderBrush="AntiqueWhite" BorderThickness="1">
                    <chartingToolkit:ColumnSeries 
                        DependentValuePath="Value" 
                        IndependentValuePath="Key" 
                        ItemsSource="{Binding}"/>
                </chartingToolkit:Chart>

“应该”填充将创建图表的列表的代码:

private void showColumnChart()
    {
        List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string,int();
        List<ProsjektTime> timeListe = new List<ProsjektTime>();

        foreach (ProsjektTime p in Master.getDC().ProsjektTimes)
        {
            if (p.ProsjektID_FK == prosjekt.ProsjektID)
            {
                timeListe.Add(p);
            }
        }

        foreach(ProsjektTime p in timeListe)
        {
            valueList.Add(new KeyValuePair<string,int(p.TimeTyper.Type,p.AntallTimer));
        }

        try
        {
            columnChart.DataContext = valueList; 
        }
        catch (InvalidOperationException e) { MessageBox.Show(e+""); }
    }

不幸的是,我收到错误消息“目前无法修改此节点的逻辑子节点,因为树行走”。

然而,我发现了一些有趣的事情。如果我将 ComboBox 的 selectedIndex 设置为注册了几个小时的项目(并非所有项目都注册了小时),那么一切正常。有小时数的项目会显示在图表中,而没有注册任何小时数的项目也会显示为空图表。我错过了什么?

4

1 回答 1

0

找到了答案。由于拥有一个 dataSourceList 和一个 chartingToolkit:ColumnSeries,我创建了两个列表和两个 chartingToolkit:ColumnSeries。继承人的代码:

        private void prosjektTimeGraf()
    {
        int estimerteTimer = 0;
        int registrerteTimer = 0;

        List<KeyValuePair<string, int>> estimerListe = new List<KeyValuePair<string, int>>();
        List<KeyValuePair<string, int>> registrertListe = new List<KeyValuePair<string, int>>();

        foreach (ProsjektTime p in Master.getDC().ProsjektTimes)
        {
            if (p.ProsjektID_FK == prosjekt.ProsjektID)
            {
                estimerteTimer += p.AntallTimer;
                estimerListe.Add(new KeyValuePair<string, int>(p.TimeTyper.Type, p.AntallTimer));
            }
        }

        foreach (ProsjektTimeBruker ptb in Master.getDC().ProsjektTimeBrukers)
        {
            if (ptb.ProsjektID_FK == prosjekt.ProsjektID)
            {
                registrerteTimer += (int)ptb.AntallTimer;
                registrertListe.Add(new KeyValuePair<string, int>(ptb.TimeTyper.Type, (int)ptb.AntallTimer));
            }
        }

        estimerListe.Insert(0, new KeyValuePair<string, int>("Totalt", estimerteTimer));
        registrertListe.Insert(0, new KeyValuePair<string, int>("Totalt", registrerteTimer));

        try
        {
            espTimer.DataContext = estimerListe;
            regpTimer.DataContext = registrertListe;
        }
        catch (InvalidOperationException e) { MessageBox.Show(e+""); }
    }
于 2013-04-05T12:13:22.333 回答