WPF: How to go to the visual tree to find Model3DGroup, 3d model pressed?

I am showing several 3D models as Model3DGroups. They are surrounded by Viewport3D, which captures MouseDown events.

I want to determine which Model3DGroup model (they all have names) was clicked. I start 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 in the WriteLine command, I look at the local view to find the variable I need, but I cannot find it. Can you help me, which way do I need to take to find the group that modelvisual3d belongs to?

Here is a screenshot of the tree: alt text

+3
source share
2 answers

I did this by surrounding Model3DGroup with ModelUIElement3D.

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

MouseDown :

    private void ModelUIElement3D_MouseDown(object sender, MouseButtonEventArgs e)
    {

            if (sender == trololo)
            {
                RaiseModelClickEvent("auditorium");    
            }
            else if (sender == LogoMouseDown)
            {
                RaiseModelClickEvent("logo");
            }

    }
+1

Linq to Visual Tree, , , , Model3DGroup. ( ).

, LinqToVT, , XAML:

hitResult.VisualHit.GetType() == typeof(ModelVisual3D)
+1

Source: https://habr.com/ru/post/1766061/


All Articles