为了账号安全,请及时绑定邮箱和手机立即绑定

OxyPlot 图形不更新

OxyPlot 图形不更新

C#
梦里花落0921 2021-06-29 12:57:58
我正在使用 C#、.NET Framework 4.7 开发 WPF 应用程序。和 Oxyplot 1.0。我试图在运行时更新图形,但它没有做任何事情。我曾尝试使用ObsevableCollection,InvalidateFlag但没有成功。这是 XAML:<oxy:Plot Title="{Binding Title}" InvalidateFlag="{Binding InvalidateFlag}">    <oxy:Plot.Series>        <oxy:LineSeries ItemsSource="{Binding BestFitness}"/>        <oxy:LineSeries ItemsSource="{Binding WorstFitness}"/>        <oxy:LineSeries ItemsSource="{Binding AverageFitness}"/>    </oxy:Plot.Series></oxy:Plot>这是视图模型:public class MainViewModel : ObservableObject{    private int count;    private int _invalidateFlag;    public string Title { get; set; }    public int InvalidateFlag    {        get { return _invalidateFlag; }        set        {            _invalidateFlag = value;            RaisePropertyChangedEvent("InvalidateFlag");        }    }    public ObservableCollection<DataPoint> BestFitness { get; set; }    public ObservableCollection<DataPoint> WorstFitness { get; set; }    public ObservableCollection<DataPoint> AverageFitness { get; set; }    public ICommand StartCommand    {        get { return new DelegateCommand(Start); }    }    public ICommand RefereshCommand    {        get { return new DelegateCommand(Refresh); }    }    public MainViewModel()    {        this.Title = "Example 2";        this.BestFitness = new ObservableCollection<DataPoint>        {            new DataPoint(0, 4),            new DataPoint(10, 13),            new DataPoint(20, 15),            new DataPoint(30, 16),            new DataPoint(40, 12),            new DataPoint(50, 12)        };    }     我还需要做什么吗?
查看完整描述

2 回答

?
烙印99

TA贡献1829条经验 获得超13个赞


this.BestFitness = new ObservableCollection<DataPoint>();

...

你正在替换完整ItemsSource的情节。由于RaisePropertyChangedEvent之后调用没有视图通知,因此绑定图将无法识别更改并且图不会更新其点。


有两种可能的解决方案:


1.RaisePropertyChangedEvent替换集合后通过调用使用INotifyPropertychanged。所以


public ObservableCollection<DataPoint> BestFitness { get; set; }

应该扩展到


private ObservableCollection<DataPoint> _BestFitness;

public ObservableCollection<DataPoint> BestFintess

{

    get

    {

        return _BestFitness;

    }

    private set

    {

        _BestFitness = value;

        RaisePropertyChangedEvent(nameof(BestFintess));

    }

}

2. 不要替换整个 ObservableCollection。只需清除现有集合并再次使用它们。这意味着使用


this.BestFitniss.Clear();

代替


this.BestFitness = new ObservableCollection<DataPoint>();

两种解决方案都会通知视图有关更改和绘图将更新它的点而不使用InvalidateFlag.


请注意,ObservableCollection如本问题所述,需要使用 UI 线程更改 an 的项目。由于您正在使用其他线程来添加调用 UI 的值,例如


Application.Current.Dispatcher.BeginInvoke(() =>

    {

        BestFitness.Add(new DataPoint(count, args.BestFitness));

    });

是必须的。


查看完整回答
反对 回复 2021-07-18
  • 2 回答
  • 0 关注
  • 359 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信