anon
anon

Reputation:

How to use VisualTreeHelper#HitTest and TagVisualizer correctly

In my application I want to ensure that a TagVisualization is only displayed if the tagged object is placed on a Ellipse. So I used this code to do that:

 private void TagVisualizer_VisualizationAdded(object sender, TagVisualizerEventArgs e)
        {
            Console.WriteLine("Hitlist");

            //Notes
            if (e.TagVisualization.GetType() == typeof(NoteVisualization))
            {
                bool found = false;
                Point pt = e.TagVisualization.Center;
                hitResultsList.Clear();
                VisualTreeHelper.HitTest(RootLayer, null, new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(pt));

                if (hitResultsList.Count > 0)
                {
                    foreach (DependencyObject o in hitResultsList)
                    {
                        if (o.GetType() == typeof(Ellipse))
                        {

                            Console.WriteLine("Placed on a Sourcefile");

                            SourceFile sf = (((o as Ellipse).Tag) as SourceFile);
                            GroupBox gp = e.TagVisualization.FindName("GroupHeader") as GroupBox;
                            gp.Header = sf.getFullName();
                            e.TagVisualization.Tag = sf;

                            SurfaceButton save = e.TagVisualization.FindName("NoteSave") as SurfaceButton;
                            save.Tag = sf;

                            found = true;
                            break;
                        }



                    }
                }

                if (!found)
                {
                    e.TagVisualization.Visibility = System.Windows.Visibility.Collapsed;
                    Console.WriteLine("Placed somewhere else");
                }
            }
        }

I'm not really sure if this is the correct way, since I don't avoid that the TagVisualization is displayed, but instead I instantly set the Visibility to collpased. I think there have to be better ways to do that?

Upvotes: 0

Views: 653

Answers (1)

Robert Levy
Robert Levy

Reputation: 29083

official guidance for how to do this is shown in one of the sdk samples: http://msdn.microsoft.com/en-us/library/ee804861(v=Surface.10).aspx

-robert (former program manager for the surface dev platform)

Upvotes: 1

Related Questions