Search code examples
c#wpfwpf-controlspixelsensescatterview

ScatterViewItem.Center (X and Y) properties returning NaN


I have a XAML based ScatterView to which I'm programmatically adding more Scatterviews (henceforth known as Scatter2s).

Each of the Scatter2s work fine - they each contain ScatterViewItems which in turn contain an Ellipse shape, and these are all drawn to the Surface perfectly as wanted.

In trying to draw lines between two ellipses, I attempt to get the Scatter2.[AScatterViewItem].Center.X and .Y properties, so I can set the X1Y1,X2Y2 properties of the line, but they return as NaN. I've also tried ActualCenter which results in the same outcome.

I initially thought it was because I was trying to access these properties before the ellipses were drawn on screen, but I printed the property values after they've been drawn and same result.

I checked to ensure that each of the ScatterViewItems width and height are explicitly set before drawing, as are the ellipse width and height - just in case that would throw the calculation of a center property off.

Is there a better way to get the position of a ScatterViewItem which has not been explicitly positioned than using the Center property?

Thanks


In Node:

public void CenterToString()
{
    Console.Out.WriteLine("Actual centre: " + svi.ActualCenter.toString());
    Console.Out.WriteLine("Centre: " + svi.Center.toString());
}

In my main class

Node node_one = ((GeneNetwork) context).getNode(localNodeIds[0]);
node_one.CenterToString();

gives output:

Actual centre to string: NaN,NaN
Centre to string: NaN,NaN

The code which adds a Node to the main ScatterView (context.Items.Add(svi)) is called before the CenterToString() function.

Could it have anything to do with where I'm placing this in my SurfaceWindow class? Currently, everything is in onInitialised(), after the call to base.OnInitialised.


Solution

  • Center is not set until you set it or user moves the SVI. I would just set it since the SV just places them randomly otherwise. If you really don't want that you will need to use TransformToAncestor as in Get Absolute Position of element within the window in wpf to get the position of the center of the SVI relative to its parent SV.