WPF 工具包 - 如何刷新图表 [英] WPF Toolkit - how to refresh a chart

查看:40
本文介绍了WPF 工具包 - 如何刷新图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用 WPF Toolkit 创建了一个饼图.我想为 MS Dynamics NAV 创建一个插件.如果我在 NAV 中调用该方法:

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

我的图表没有刷新.我的 ObservableCollection 正在更新,但没有显示任何图表.如果我只是这样做

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

setChart("AB123",60);

到它工作的构造函数.

如何更新图表.我还在构造函数中调用 pieChart.DataContext = values; .如果我在 setChart 中再次调用它,它仍然不起作用.

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.

点击我:截图

推荐答案

您在初始化窗口后设置 values 并且因为 values 在您的示例中没有实现setter 和 INotifyPropertyChanged 方式,您的 UI 线程永远不会因您对集合所做的更改而发出警告.

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.

使用INotifyPropertyChanged接口:

就像当你设置你的项目时,你的 UI 线程知道在 xaml 部分有一个改变(我拿了一个窗口,但它可以是一个页面、一个用户控件或一个自定义类)

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

....

我在您的 xaml 中没有看到您的代码,也许这里也有更改.

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

这篇关于WPF 工具包 - 如何刷新图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆