简体   繁体   中英

How to bind collection to chart series itemsource (WPF)

Right now, I have this code:

xaml:

 <DVC:Chart Name="MyChart" Background="LightBlue" Margin="10">
            <DVC:Chart.Series>
                <DVC:BarSeries Title="Sumele"
                    IndependentValueBinding="{Binding Path=Key}"
                    DependentValueBinding="{Binding Path=Value}">
                </DVC:BarSeries>
            </DVC:Chart.Series>
        </DVC:Chart>

xaml.cs:

using System.Windows.Controls.DataVisualization.Charting;

     private void LoadBarChartData()
                {
                    ((BarSeries)MyChart.Series[0]).ItemsSource =
                        new KeyValuePair<string, int>[]{
                        new KeyValuePair<string,int>("Project Manager", 12),
                        new KeyValuePair<string,int>("CEO", 25),
                        new KeyValuePair<string,int>("Software Engg.", 5)    
                }

Is there a way to bind somehow for example LIST? or DataTable? That will contain all information that I wrote above...

((BarSeries)MyChart.Series[0]).ItemsSource = (Here)

Thanks

Well, I found solution, how to create that object and insert collection using for loop

    int t = 10;
    KeyValuePair<string, int>[] cp = new KeyValuePair<string, int>[t];

    for (int i = 0; i < 10; i++)
    {
        cp[i] = new KeyValuePair<string, int>("Project Manager", i);
    }
    ((ColumnSeries)MyChart.Series[0]).ItemsSource = cp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM