Диаграмма Silverlight 4, созданная во время выполнения, не отображает данные - PullRequest
0 голосов
/ 06 февраля 2012

Используя элемент управления диаграммой инструментария Silverlight 4, я пытаюсь создать диаграмму на 100% во время выполнения без каких-либо доказательств этого в XAML. Для этого я создаю пустой график при загрузке страницы:

        Chart TrendChart = new Chart();
        TrendChart.Name = "TrendChart";
        TrendChart.Title = "Call History";
        TrendChart.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
        TrendChart.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
        TrendChart.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
        TrendChart.VerticalContentAlignment = System.Windows.VerticalAlignment.Stretch;

        GridPanel.Children.Add(TrendChart);

После того, как пользователь нажимает кнопку для извлечения данных, создается список этого пользовательского класса:

    private class PhoneTrendDataPoint
    {
        public string XValue { get; set; }
        public double YValue { get; set; }
    }

Я использую этот список, который называется CurrentCallTrends, в качестве источника элементов для моего графика.

        // Update the chart with the received data
        Chart TrendChart = (Chart)this.FindName("TrendChart");

        // Wipe out previous chart data
        TrendChart.Series.Clear();



        // set the data
        ColumnSeries columnSeries = new ColumnSeries();
        columnSeries.Name = "Current Call Volume";
        columnSeries.ItemsSource = CurrentCallTrends;
        //columnSeries.SetBinding(ColumnSeries.ItemsSourceProperty, new Binding("CurrentCallTrends"));
        columnSeries.DependentValueBinding = new Binding("XValue");
        columnSeries.IndependentValueBinding = new Binding("YValue");
        TrendChart.Series.Add(columnSeries);

Проблема заключается в том, что я получаю ошибку во время выполнения, когда мне предлагается открыть отладчик относительно ссылки на объект, не установленной для экземпляра объекта. Если я прокомментирую строку в .SetBinding, то ItemsSource исчезнет, ​​и данные не отобразятся, но, по крайней мере, ошибки времени выполнения не будет.

Чего мне не хватает?

1 Ответ

0 голосов
/ 06 февраля 2012

После дополнительного поиска в Google, я сделал несколько модификаций, которые, кажется, работают, но не кажутся мне лучшим способом сделать это. Данные теперь отображаются, но я не приму это как ответ, если нет лучшего метода:

        // Update the chart with the received data
        Chart TrendChart = (Chart)this.FindName("TrendChart");

        // Wipe out previous chart data
        TrendChart.Series.Clear();


        // test data
        KeyValuePair<string, double>[] CurrentCallData = new KeyValuePair<string, double>[CurrentCallTrends.Count];
        for (int i = 0; i < CurrentCallTrends.Count; i++)
        {
            CurrentCallData[i] = new KeyValuePair<string, double>(CurrentCallTrends[i].XValue, CurrentCallTrends[i].YValue);
        }



        // set the data
        ColumnSeries columnSeries = new ColumnSeries();
        columnSeries.Name = "CurrentCallVolume";
        columnSeries.Title = "Current Call Volume";
        columnSeries.SetBinding(ColumnSeries.ItemsSourceProperty, new Binding());
        //columnSeries.ItemsSource = CurrentCallTrends;
        columnSeries.ItemsSource = CurrentCallData;
        columnSeries.DependentValueBinding = new Binding("Value");
        columnSeries.IndependentValueBinding = new Binding("Key");
        TrendChart.Series.Add(columnSeries);

        //this.DataContext = CurrentCallTrends;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...