简体   繁体   中英

C# OxyPlot Textposition of PolygonAnnotation

I am having two PolygonAnnotation shown on a PlotModel . I want to connect them via one ArrowAnnotation . As Startpoint and Endpoint of the ArrowAnnotation I want to have a defined point of respective PolygonAnnotation , not the clicked position. As the Text of each PolygonAnnotation is already printed in the center via TextHorizontalAlignment and TextVerticalAlignment I thought using TextPosition as the DataPoint for the respective Startpoint or Endpoint would be a good idea. Unfortunatly TextPosition always equals {n. def. n. def.} {n. def. n. def.} {n. def. n. def.} .

Any ideas how to get the position of the Text or another defined Datapoint within a PolygonAnnotation ?

Not sure if this is the best way, but you could try working your way through the Visual Tree.

    public void FindPosition(object obj)
    {
        if (obj == null) return;
        var plotView = (PlotView)obj;
        FindTextPosition<PlotView>(plotView).FindName(plotView.Name);
    }
    private IEnumerable<T> FindTextPosition<T>(DependencyObject parent)
        where T : DependencyObject
    {
        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            if (child is TextBlock text)
            {
                if (text.Text == "Test") // Or any identifier you could specify
                {
                    var position = text.PointToScreen(new Point(0d, 0d));

                }

            }

            foreach (var other in FindTextPosition<T>(child)) yield return other;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM