keelerjr12
keelerjr12

Reputation: 1903

WPF Binding Problems

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>>();
}

Upvotes: 0

Views: 39

Answers (1)

Grant Winney
Grant Winney

Reputation: 66449

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

Upvotes: 1

Related Questions