OxyPlot 图形不更新

我正在使用 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)

        };

    }


     

我还需要做什么吗?


梦里花落0921
浏览 360回答 2
2回答

烙印99

和this.BestFitness = new ObservableCollection<DataPoint>();...你正在替换完整ItemsSource的情节。由于RaisePropertyChangedEvent之后调用没有视图通知,因此绑定图将无法识别更改并且图不会更新其点。有两种可能的解决方案:1.RaisePropertyChangedEvent替换集合后通过调用使用INotifyPropertychanged。所以public ObservableCollection<DataPoint> BestFitness { get; set; }应该扩展到private ObservableCollection<DataPoint> _BestFitness;public ObservableCollection<DataPoint> BestFintess{&nbsp; &nbsp; get&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _BestFitness;&nbsp; &nbsp; }&nbsp; &nbsp; private set&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _BestFitness = value;&nbsp; &nbsp; &nbsp; &nbsp; RaisePropertyChangedEvent(nameof(BestFintess));&nbsp; &nbsp; }}2. 不要替换整个 ObservableCollection。只需清除现有集合并再次使用它们。这意味着使用this.BestFitniss.Clear();代替this.BestFitness = new ObservableCollection<DataPoint>();两种解决方案都会通知视图有关更改和绘图将更新它的点而不使用InvalidateFlag.请注意,ObservableCollection如本问题所述,需要使用 UI 线程更改 an 的项目。由于您正在使用其他线程来添加调用 UI 的值,例如Application.Current.Dispatcher.BeginInvoke(() =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; BestFitness.Add(new DataPoint(count, args.BestFitness));&nbsp; &nbsp; });是必须的。
打开App,查看更多内容
随时随地看视频慕课网APP