0

I'm using WPF Toolkit's Charting, but I'm having a problem with getting it to bind to my ViewModel. Nothing shows up. I have the MainWindow.DataContext = MainWindowViewModel if you're wondering. This is what I have:

MainWindow.xaml

<chartingToolkit:Chart Grid.Row="2">
    <chartingToolkit:ColumnSeries Name="line_chart" 
                                  IndependentValuePath="Key"
                                  DependentValuePath="Value" 
                                  ItemsSource="{Binding Me}"/>
</chartingToolkit:Chart>

MainWindowViewModel.cs

class MainWindowViewModel
{
    public List<KeyValuePair<string, int>> Me { get; set; }

    public MainWindowViewModel(Model model)
    {
        this.model = model;

        me.Add(new KeyValuePair<string, int>("test", 1));
        me.Add(new KeyValuePair<string, int>("test1", 1000));
        me.Add(new KeyValuePair<string, int>("test2", 20));
        me.Add(new KeyValuePair<string, int>("test3", 500));
    }

    Model model;

    List<KeyValuePair<string, int>> me = new ObservableCollection<KeyValuePair<string,int>>();
}

1 Answer 1

1

I haven't used that chart tool before, but you're binding to a public Me property that has no reference whatsoever to the private me field that you're adding values to.

Remove the private field and try this instead:

class MainWindowViewModel
{
    public ObservableCollection<KeyValuePair<string, int>> Me { get; private set; }

    public MainWindowViewModel(Model model)
    {
        this.model = model;

        // Instantiate in the constructor, then add your values
        Me = new ObservableCollection<KeyValuePair<string, int>>();

        Me.Add(new KeyValuePair<string, int>("test", 1));
        Me.Add(new KeyValuePair<string, int>("test1", 1000));
        Me.Add(new KeyValuePair<string, int>("test2", 20));
        Me.Add(new KeyValuePair<string, int>("test3", 500));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm an idiot... Didn't even catch that. Thanks, works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.