Search code examples
c#wpfvisual-studiowpftoolkitdynamics-nav

WPF Toolkit - how to refresh a chart


I just created a pie Chart with the WPF Toolkit. I wanted to create a AddIn for MS Dynamics NAV. If I call that method in NAV:

    public void setChart(string chartKey, float chartValue)
    {
        KeyValuePair<string, float> value = new KeyValuePair<string, float>(chartKey, chartValue);
        values.Add(value);
    }

my Chart is not refreshing. My ObservableCollection is updating but it doesn't Show any Chart. If I just do

setChart("AB123",60);

to the constructor it works.

How can I update the Chart. I also call pieChart.DataContext = values; in the constructor. If I call it again in setChart it still not work.

Click me: Screenshot


Solution

  • You set your values after initializing your windows and since values in your example doesn't implement a setter and the INotifyPropertyChanged manner, your UI thread is never warn by the change you made on your collection.

    Use INotifyPropertyChanged interface:

    Like that when you set your items, Your UI thread knows there is a change to do in the xaml part (I took a Window but it can be a Page, a UserControl or a Custom Class)

    public partial class MainWindow : Window, INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        private ObservableCollection<KeyValuePair<string, float>> _values;
        public ObservableCollection<KeyValuePair<string, float>> values {
            get {
                if (_values == null) {
                    _values = new ObservableCollection<KeyValuePair<string, float>>();
                }
                return _values;
            }
            set {
                _values = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(values)));
            }
        }
    
    ....
    

    I didn't see your code in your xaml maybe there is a change to do here too.