I'm using the MVVM pattern, I have the mouse position on my Plot
in my model through the attachment to a mouse behaviour.
I am using a DateTimeAxis
for the x-axis, I would like to get the x-axis value from my x-position, but I don't know how to proceed.
If I was not using the MVVM pattern, a good way of accomplishing what I want, would be:
XAML
<oxy:Plot x:Name="TopPlot" MouseMove="TopPlot_MouseMove" >
<oxy:Plot.Axes>
<oxy:DateTimeAxis x:Name="DateAxis" Position="Bottom" />
<oxy:LinearAxis x:Name="ValueAxis" Title="Value" Position="Left"/>
</oxy:Plot.Axes>
</oxy:Plot>
Code behind:
private void TopPlot_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
var x_axis = this.TopPlot.ActualModel.DefaultXAxis;
var y_axis = this.TopPlot.ActualModel.DefaultYAxis;
var point = OxyPlot.Axes.Axis.InverseTransform(new ScreenPoint(e.GetPosition(TopPlot).X,
e.GetPosition(TopPlot).Y), x_axis, y_axis);
}
I tried to do a OneWayToSource
binding with the property Model of the Plot (so that I could do something linke in the non-MVVM model), but the value I receive in my property is null
.
XAML
<oxy:Plot Model="{Binding Path=Plot_Model, Mode=OneWayToSource}" >
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding m_Series,
UpdateSourceTrigger=PropertyChanged}"/>
</oxy:Plot.Series>
<oxy:Plot.Axes>
<oxy:DateTimeAxis Position="Bottom"/>
<oxy:LinearAxis Position="Left" Title="Value"/>
</oxy:Plot.Axes>
<i:Interaction.Behaviors>
<mouseMoveMvvm:MouseBehaviour MouseX="{Binding PlotX, Mode=OneWayToSource}"
MouseY="{Binding PlotY, Mode=OneWayToSource}"/>
</i:Interaction.Behaviors>
</oxy:Plot>
Code in my Model:
private double _plotX;
public double PlotX
{
get { return _plotX; }
set
{
if (value.Equals(_plotX)) return;
_plotX = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(PositionText));
}
}
private double _plotY;
public double PlotY
{
get { return _plotY; }
set
{
if (value.Equals(_plotY)) return;
_plotY = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(PositionText));
}
}
private PlotModel m_model;
public PlotModel Plot_Model
{
set
{
m_model = value;
}
}
public string PositionText
{
get
{
//var x_axis = m_model.DefaultXAxis;
//var y_axis = m_model.DefaultYAxis;
//DataPoint point = Axis.InverseTransform(new ScreenPoint(_plotX, _plotY), x_axis,
// y_axis);
//return string.Format("Pos. X: {0}, /n Pos. Y: {1} ", point.X, point.Y);
return string.Format("Pos. X: {0}, /n Pos. Y: {1} ", _plotX, _plotY);
}
}
Does anyone have any suggestions on how to proceed?