在 WPF 中使用数据绑定时 OxyPlot 不刷新 [英] OxyPlot not refreshing when using data binding in WPF
问题描述
我正在异步获取数据并尝试通过 LineSeries 填充绘图,但在更新绑定集合 (ObservableCollection) 时绘图不会刷新.注意:当绑定集合更改时,我有一个 XAML 行为来调用 InvalidatePlot(true).
I'm asynchonrously getting data and attempting to populate a plot via the LineSeries, except the plot does not refresh when the bound collection (ObservableCollection) is updated. Note: I have a XAML behavior to call InvalidatePlot(true) when the bound collection changes.
谁能解释为什么情节没有按预期更新?
WPF.Net 4.0OxyPlot 2014.1.293.1
WPF .Net 4.0 OxyPlot 2014.1.293.1
我有以下 XAML 数据模板,您可以看到 LineSeries ItemsSource 绑定到 ViewModel 中的属性 (PlotData):
I have the following XAML datatemplate, as you can see the LineSeries ItemsSource is bound to a property (PlotData) in the ViewModel:
<DataTemplate DataType="{x:Type md:DataViewModel}">
<Grid>
<oxy:Plot x:Name="MarketDatePlot"
Margin="10">
<oxy:Plot.Axes>
<oxy:DateTimeAxis Position="Bottom"
StringFormat="dd/MM/yy"
MajorGridlineStyle="Solid"
MinorGridlineStyle="Dot"
IntervalType="Days"
IntervalLength="80" />
<oxy:LinearAxis Position="Left"
MajorGridlineStyle="Solid"
MinorGridlineStyle="Dot"
IntervalLength="100" />
</oxy:Plot.Axes>
<oxy:LineSeries ItemsSource="{Binding Path=PlotData, Mode=OneWay}" />
<i:Interaction.Behaviors>
<behaviors:OxyPlotBehavior ItemsSource="{Binding Path=PlotData, Mode=OneWay}" />
</i:Interaction.Behaviors>
</oxy:Plot>
</Grid>
</DataTemplate>
正如我所说,ViewModel 异步请求并填充绑定集合(绑定集合的实际填充发生在 UI 线程上):
As I said the ViewModel requests and populates the bound collection asynchronously (the actual population of bound collection happens on the UI thread):
public sealed class DataViewModel : BaseViewModel, IDataViewModel
{
private readonly CompositeDisposable _disposable;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly RangeObservableCollection<DataPoint> _plotData;
public DataViewModel(DateTime fromDate, DateTime toDate, IMarketDataService marketDataService, ISchedulerService schedulerService)
{
_plotData = new RangeObservableCollection<DataPoint>();
_disposable = new CompositeDisposable();
if (fromDate == toDate)
{
// nothing to do...
return;
}
_cancellationTokenSource = new CancellationTokenSource();
_disposable.Add(Disposable.Create(() =>
{
if (!_cancellationTokenSource.IsCancellationRequested)
{
_cancellationTokenSource.Cancel();
}
}));
marketDataService.GetDataAsync(fromDate, toDate)
.ContinueWith(t =>
{
if (t.IsFaulted)
{
throw new Exception("Failed to get market data!", TaskHelper.GetFirstException(t));
}
return t.Result.Select(x => new DataPoint(DateTimeAxis.ToDouble(x.Time), x.Value));
}, schedulerService.Task.Default)
.SafeContinueWith(t => _plotData.AddRange(t.Result), schedulerService.Task.CurrentSynchronizationContext);
}
public void Dispose()
{
_disposable.Dispose();
}
public IEnumerable<DataPoint> PlotData
{
get { return _plotData; }
}
}
XAML 行为如下所示:
And the XAML behavior looks like this:
(我似乎不能再粘贴代码了,所以保存时总是抛出错误)
(I can't seem to paste in anymore code, SO keeps throwing an error on save)
推荐答案
添加数据时,OxyPlot 不会自动更新.
OxyPlot does not automatically update when you add data.
你必须调用 plotname.InvalidatePlot(true);
You must call plotname.InvalidatePlot(true);
并且它必须运行在 UI 调度程序线程上,即
and it must run on the UI dispatcher thread, ie
Dispatcher.InvokeAsync(() =>
{
plotname.InvalidatePlot(true);
}
这篇关于在 WPF 中使用数据绑定时 OxyPlot 不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!