2013-04-03 4 views
1

J'ai un ComboBox avec différents projets. Basé sur la valeur de cette liste déroulante, je veux que les différentes heures qui ont été enregistrées sur ce projet présenté dans un tableau.Créer un graphique avec WPF Chart ToolKit basé sur le ComboBox sélectionné

Le code pour le tableau que j'ai créé:

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

Le code qui « devrait » remplir la liste qui créerait le graphique:

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+""); } 
    } 

Malheureusement je reçois l'erreur, « Impossible modifiez les enfants logiques pour ce noeud à ce moment car un arbre marche ".

Cependant, j'ai découvert quelque chose d'intéressant. Si je mets l'attribut selectedIndex de la ComboBox dans un projet qui a quelques heures d'enregistrement (tous les projets n'ont pas d'heures d'enregistrement), tout fonctionne correctement. Les projets dont les heures sont présentées dans le tableau et les projets sans heures d'inscription sont également présentés sous forme de tableau vide. Qu'est-ce que je rate?

Répondre

0

J'ai trouvé la réponse. Insted d'avoir un dataSourceList et un chartingToolkit: ColumnSeries, j'ai créé deux listes et deux chartingToolkit: ColumnSeries. Voici le code:

 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+""); } 
    } 
Questions connexes