0

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?

thatguy
  • 21,059
  • 6
  • 30
  • 40

1 Answers1

0

I do not know where you got the MouseBehavior from, but it might be incompatible with Plot. I think the easiest way is to create your own behavior that fits your requirements, since you already have a working code-behind solution. If you translate it into a behavior, it would look like this.

public class PlotMousePositionBehaviour : Behavior<Plot>
{
   public static readonly DependencyProperty YProperty = DependencyProperty.Register(
      nameof(Y), typeof(double), typeof(PlotMousePositionBehaviour));

   public static readonly DependencyProperty XProperty = DependencyProperty.Register(
      nameof(X), typeof(double), typeof(PlotMousePositionBehaviour));

   public double Y
   {
      get => (double)GetValue(YProperty);
      set => SetValue(YProperty, value);
   }

   public double X
   {
      get => (double)GetValue(XProperty);
      set => SetValue(XProperty, value);
   }

   protected override void OnAttached()
   {
      AssociatedObject.MouseMove += OnMouseMove;
   }

   protected override void OnDetaching()
   {
      AssociatedObject.MouseMove -= OnMouseMove;
   }

   private void OnMouseMove(object sender, MouseEventArgs e)
   {
      var xAxis = AssociatedObject.ActualModel.DefaultXAxis;
      var yAxis = AssociatedObject.ActualModel.DefaultYAxis;

      var screenPoint = new ScreenPoint(e.GetPosition(AssociatedObject).X, e.GetPosition(AssociatedObject).Y);
      var point = OxyPlot.Axes.Axis.InverseTransform(screenPoint, xAxis, yAxis);

      X = point.X;
      Y = point.Y;
   }
}

You could further extend it to add a property for a custom converter instead of hard-wiring the point conversion. With this behavior you can bind the X and Y coordinates to your view model.

<oxy:Plot x:Name="TopPlot">
   <b:Interaction.Behaviors>
      <local:PlotMousePositionBehaviour X="{Binding PlotX}"
                                        Y="{Binding PlotY}"/>
   </b:Interaction.Behaviors>
   <oxy:Plot.Axes>
      <oxy:DateTimeAxis x:Name="DateAxis" Position="Bottom" />
      <oxy:LinearAxis x:Name="ValueAxis" Title="Value" Position="Left"/>
   </oxy:Plot.Axes>
</oxy:Plot>

Regarding your PositionText, this should be a matter of the view. You could instead bind X and Y directly in the view and format the text. Here is an example, assign an x:Name to the behavior.

<b:Interaction.Behaviors>
   <local:PlotMousePositionBehaviour x:Name="PlotMousePositionBehaviour"/>
</b:Interaction.Behaviors>

Reference the properties via ElementName in a TexBlock (alternatively use StringFormat).

<TextBlock>
   <Run Text="{Binding X, ElementName=PlotMousePositionBehaviour, StringFormat={}Pos. X: {0}}"/>
   <LineBreak/>
   <Run Text="{Binding Y, ElementName=PlotMousePositionBehaviour, StringFormat={}Pos. Y: {0}}"/>
</TextBlock>

This way, you do not even need the indirection over the view model, if the values are not used there.

thatguy
  • 21,059
  • 6
  • 30
  • 40