简体   繁体   中英

Simple Bar Chart with WPF Toolkit?

I've tried to follow this example for a simple bar chart with MVVM:

Code Project MVVM Charting

But cannot get any chart to display. Here is my view model which exposes a KeyValuePair ObservableCollection :

 private ObservableCollection<KeyValuePair<string, int>> _ChartParametersPairedObservableList;
    public ObservableCollection<KeyValuePair<string, int>> ChartParametersPairedObservableList
    {
        get { return _ChartParametersPairedObservableList; }
        set
        {
            _ChartParametersPairedObservableList = value;
            OnPropertyChanged("ChartParametersPairedObservableList");
        }
    }

and instantiates/populates the collection from another ObservableCollection :

private void GetChartParametersAndBuildKeyValuePairs()
    {
        ChartParametersObservableList = new ObservableCollection<ChartParameters>(IsesService.GetChartParameters(Country, ArticleType));
        ChartParametersPairedObservableList = new ObservableCollection<KeyValuePair<string, int>>();

        foreach (var item in ChartParametersObservableList)
        {              
            ChartParametersPairedObservableList.Add(new KeyValuePair<string, int>(item.operator_name_abrv, item.countAll));
        }
    }

This collection works as expected. I know property changed is working as I can bind a DataGrid to the collection from the view no problem. The issue is when I try to bind to a chart.

Here is my view Xaml for the chart:

xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

<DVC:Chart Canvas.Top="80" Canvas.Left="10" ItemsSource="{Binding ChartParametersPairedObservableList}" Width="400" Height="250">
            <DVC:Chart.Series>
                <DVC:BarSeries ItemsSource="{Binding ChartParametersPairedObservableList}"
                               IndependentValueBinding="{Binding Path=Key}" 
                               DependentValueBinding="{Binding Path=Value}">
                    </DVC:BarSeries>
            </DVC:Chart.Series>
            </DVC:Chart>

Unfortunately, I don't get any data output in my chart at run time. I'm getting binding errors in the output window. Can't bind to the value & key:

BindingExpression path error: 'Key' property not found on 'object'
BindingExpression path error: 'Value' property not found on 'object'

What Am I doing wrong?

Solved this by using Modern UI (Metro) Charts for Windows 8, WPF, Silverlight.

modernuicharts

Great library & modern look, no more Windows 95 styling!

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