I'm simply trying to plot a Heatmap series using a 2D array of data stored in Cpdata. I've left out the parts not useful to the question. I'm new to wpf and oxyplot and imagine I'm simply making a mistake with bindings somewhere. The xaml code is also below and simplified.
At the moment I am getting a blank window when I run the project. Please let me know if anything is unclear and thanks!
<Window x:Class="Project.Flowfield"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:local="clr-namespace:Project"
Title="Flowfield" Height="850" Width="1200" MinHeight="700" MinWidth="330"
<Window.DataContext>
<local:OxyPlotModel/>
</Window.DataContext>
<Grid>
<oxy:PlotView x:Name="Cpheatmap" Model="{Binding PlotModel}">
</oxy:PlotView>
</Grid>
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Axes;
namespace Project
{
public class OxyPlotModel : INotifyPropertyChanged
{
private OxyPlot.PlotModel plotModel; //field
public OxyPlot.PlotModel PlotModel //property
{
get
{
return plotModel;
} //get method
set
{
plotModel = value;
OnPropertyChanged("PlotModel");
} //set method
}
public OxyPlotModel()
{
PlotModel = new PlotModel();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler!= null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
public partial class Flowfield : Window
{
private OxyPlotModel oxyPlotModel;
public Flowfield()
{
oxyPlotModel = new OxyPlotModel();
DataContext = oxyPlotModel;
InitializeComponent();
}
public double[,] Cpdata = new double[50, 50];
public void RunCommand()
{
//...
//Cpdata is a 2D array calculated from another method (skipped because irrelevant)
//...
var oxyPlotModel = new PlotModel();
var hms = new HeatMapSeries { Data = Cpdata};
oxyPlotModel.Series.Add(hms);
DataContext = this;
}
}
}
In RunCommand()
you reset the datacontext of the window (flowfield) to itself. The DataContext needs to be an OxyPlotModel because your PlotView is binding to its PlotModel property.
Also, and it's impossible to tell considering that you said you removed some code, you shouldn't even need to create a new instance of OxyPlotModel in the command. Just set the relevant properties or clear/add series in the existing instance, and since it raises change notifications in its property setters, the bindings will update.