Kugelschreiber_Y Kugelschreiber_Y - 3 months ago 49
C# Question

Oxyplot does not display plots in WPF view

I'm working with OxyPlot in WPF. Trying to display PlotModels in a UI.
Sometimes the plot is displayed in the UI and sometimes not.

This code is used to initialize the

PlotModel
by a
BackgroundWorker
:

ViewModel:

this.pm = setUpModel();
data2plot();


With the methods:

private PlotModel _pm;
public PlotModel pm
{
get { return _pm; }
set { _pm= value; RaisePropertyChanged("pm"); }
}


private PlotModel setUpModel()
{
PlotModel pm = new PlotModel();
pm.IsLegendVisible = false;
var xAxis= new CategoryAxis();
pm.Axes.Add(xAxis);
var valueAxis = new LinearAxis();
pm.Axes.Add(valueAxis);
return pm ;
}

//tbl holds the data in the second column
private void data2plot(DataTable tbl)
{
var series = new OxyPlot.Series.LineSeries();
int i = 0;
foreach (DataRow row in tbl.Rows)
{
double val = double.Parse(row[1].ToString());
series.Points.Add(new DataPoint(i, val));
i++;
}

this.pm.Series.Add(series);
}


Code used in View to initialize the model:

<Grid HorizontalAlignment="Left" Height="205" Margin="10,117,0,0" VerticalAlignment="Top" Width="308">
<oxypl:PlotView x:Name="plot" Model="{Binding pm,UpdateSourceTrigger=PropertyChanged}" Margin="10" Grid.Row="1">
</oxypl:PlotView>
</Grid>


with the following references

xmlns:oxypl="http://oxyplot.org/wpf"
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModel}"


The Binding works, so I guess there have to be a problem with my
PlotModel
.

I don't know if this is relevant, but the
DataTable tbl
is initialized by the
Lazy<T>
class (with
isThreadSafe=true
).

Answer

When you add to your pm member, the value of pm is not being updated - it's the content of that member that is updated, i.e. the setter is never called, so the RaisePropertyChanged event is never emitted and the view isn't notified of any change. Suggest you build a local set of data and then overwrite pm with a complete set of data at the end, or emit a RaisePropertyChanged event when you're done.

The reason that it is working sometimes I would guess is that the first (and only) time the view reads the content back from the viewmodel you've already populated it, so you see something, but then no updates.