开发者

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

开发者 https://www.devze.com 2023-01-17 08:50 出处:网络
I\'m displaying a few 3D-models as Model3DGroups. They are surrounded by Viewport3D which catches MouseDown-events.

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 whi开发者_JS百科ch path I need to take to find the group, the modelvisual3d belongs to?

Here is a screenshot of the tree:

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


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");
            }

    }


You could use Linq to Visual Tree as it doesn't matter whether the named element you are looking for is Model3DGroup. It is just another Dependency Object (if I understand your question).

Check result's type and then LinqToVT to go up, retrieving it's XAML predecessor:

hitResult.VisualHit.GetType() == typeof(ModelVisual3D)
0

精彩评论

暂无评论...
验证码 换一张
取 消