WPF Mouse Interactivity


https://www.nevron.com/Forum/Topic9343.aspx
Print Topic | Close Window

By Gil Lund - 9 Years Ago
Hello,  I'm trying to get my chart control to respond to the MouseDown click event.  I see the examples for implementing in Winforms but I can't get the event handler in the control to fire.  I can get a generic event handler to fire from within other elements of the window.

<wpf:NTimelineChartControl x:Name="nChartControl1" Background="Transparent" MouseDown="NChartControl1_OnMouseDown" IsHitTestVisible="True"/>

Thx,

Gil.
By Nevron Support - 9 Years Ago
Hi Gil,
You need to download the latest version (build 15.8.25.12), which features a new WPF control that derives directly from FrameworkElement and does not use a WinForm control host anymore. Events should be working there just like with every other WPF element.
By Gil Lund - 9 Years Ago
Thanks.  The events now fire after I upgraded.  I am however not getting an accurate hit result.  Is the code below an accurate way to get the mouse position and resolve it to the chart element?  Thx, Gil.

  private void OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
   {
StringBuilder strBuild = new StringBuilder();


    System.Windows.Point pos = new System.Windows.Point();

    pos = e.GetPosition(this);

    Debug.WriteLine(String.Format("X: {0}, Y: {1}", pos.X, pos.Y));

    //MouseEventTextBox.Text = mouseEvent;
    //ButtonTextBox.Text = e.Button.ToString();
    //ChartObjectTextBox.Text = sender.GetType().ToString();
    string sInfo = GetResultDescription(this.HitTest((int)pos.X, (int)pos.Y));
    Debug.WriteLine(sInfo);
    //DescriptionTextBox.Text = sInfo;
   }

   private string GetResultDescription(NHitTestResult hitTestResult)
   {
    int nChartElement = (int)hitTestResult.ChartElement;
    string sInfo = "";
    Debug.WriteLine(hitTestResult.ChartElement.ToString());

    switch (hitTestResult.ChartElement)
    {
      case ChartElement.Nothing:
       sInfo = "Nothing";
       break;
      case ChartElement.ControlBackground:
       sInfo = "Control background";
       break;
      case ChartElement.DataPoint:
       sInfo = "Data point [" + hitTestResult.DataPointIndex.ToString() + "] from series [" + ((NSeriesBase)hitTestResult.Object.ParentNode).Name + "]";
       break;
      case ChartElement.SurfaceDataPoint:
       sInfo = "Surface data point [" + hitTestResult.SurfaceDataPointX.ToString() + ", " + hitTestResult.SurfaceDataPointZ.ToString();
       break;
      case ChartElement.Axis:
       sInfo = "Axis [" + hitTestResult.ObjectIndex + "]";
       break;
      case ChartElement.ChartWall:
       sInfo = "Wall [" + hitTestResult.ObjectIndex + "]";
       break;
      case ChartElement.Legend:
       sInfo = "Legend";
       break;
      case ChartElement.LegendDataItem:
       sInfo = "Legend data item [" + hitTestResult.ObjectIndex.ToString() + "]";
       break;
      case ChartElement.LegendHeader:
       sInfo = "Legend header";
       break;
      case ChartElement.LegendFooter:
       sInfo = "Legend footer";
       break;
      case ChartElement.AxisStripe:
       sInfo = "Axis stripe";
       break;
      case ChartElement.Label:
       sInfo = "Label";
       break;
      case ChartElement.Watermark:
       sInfo = "Watermark";
       break;
      case ChartElement.Annotation:
       sInfo = "Annotation";
       break;
      case ChartElement.Chart:
       sInfo = "Chart";
       break;
      default:
       Debug.Assert(false); // new chart element?
       break;
    }

    return sInfo;
   }
By Nevron Support - 9 Years Ago
Hi Gil,
You need to pass the position relative to the chart control - for example:

        /// <summary>
        /// Converts a dip to pixel
        /// </summary>
        /// <param name="dip"></param>
        /// <returns></returns>
        internal int Dip2Px(double dip)
        {
            double factor = (NResolution.ScreenResolution.DpiX / 96.0);
            return (int)Math.Round(dip * factor);
        }

        private void NChartControl1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            System.Windows.Point pos = e.GetPosition(nChartControl1);
            NHitTestResult result = nChartControl1.HitTest(Dip2Px(pos.X), Dip2Px(pos.Y));
        }

We'll also add a few helper methods to facilitate that...