I am using OxyPlot to draw some diagrams in my .NET WPF project. MVVM is a new thing to me, so i am not sure how to follow it in this situation.
- There is a window (main view) that contains PlotView.
- There is a model that contains a collection of objects. Each object corresponds to a graph on my plot.
- There is a view model which creates
LineSeries
, puts it intoPlotModel
.
The problem is that i want each graph to be clickable. After a click another window opens with more detailed information on the corresponding object. My current view model is something like this:
public class MainViewModel : BaseViewModel
{
public PlotModel MyPlotModel { get; set; }
private MyModel model;
public MainViewModel()
{
model = new MyModel();
DrawGraphs();
}
private void DrawGraphs()
{
List<MyObject> myObjects = model.GetObjects();
foreach (var obj in myObjects)
{
var s = new LineSeries();
// Setting up datapoints and appearance of the graph
s.MouseDown += (object sender, OxyMouseDownEventArgs e) =>
{
var view = new ObjectView(); // A view with full information of this specific object
view.DataContext = new ObjectViewModel(obj); // View model uses the object as a parameter to do bindings for new view
view.ShowDialog();
};
MyPlotModel.Series.Add(s);
}
OnPropertyChanged("MyPlotModel");
}
}
MouseDown can be set up only on series creation because i need to know a specific object from my model that corresponds to this graph.
It works fine to me, however, i doubt that implementation follows MVVM. View model should not be able to create new views. But i cannot come up with a better solution. Is there a more elegant and MVVM-ish way to do this?
SomgBird is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.