Search code examples
c#.netwpfvisual-tree

WPF: How to walk up the Visual Tree to find a Model3DGroup a clicked 3d-model is in?


I'm displaying a few 3D-models as Model3DGroups. They are surrounded by Viewport3D which catches MouseDown-events.

I want to determine which Model3DGroup (they all have names) was clicked. I'm starting with this:

        Point location = e.GetPosition(karte.ZAM3DViewport3D);
        HitTestResult hitResult = VisualTreeHelper.HitTest(karte.ZAM3DViewport3D, location);

        if (hitResult != null )
        {
            Debug.WriteLine("BREAKPOINT");
            // Hit the visual.
        }

After hitting the breakpoint set at the WriteLine-command I'm looking at the local-view to find the right variable but can't find it. Can you help me out which path I need to take to find the group, the modelvisual3d belongs to?

Here is a screenshot of the tree: alt text


Solution

  • I did it by surrounding the Model3DGroup with a ModelUIElement3D.

    <ModelUIElement3D MouseDown="ModelUIElement3D_MouseDown" x:Name="LogoMouseDown">
    

    the MouseDown-function handles it this way:

        private void ModelUIElement3D_MouseDown(object sender, MouseButtonEventArgs e)
        {
    
                if (sender == trololo)
                {
                    RaiseModelClickEvent("auditorium");    
                }
                else if (sender == LogoMouseDown)
                {
                    RaiseModelClickEvent("logo");
                }
    
        }