简体   繁体   中英

How do I make ContextMenu in WPF throwing while Model3D objects touched only

I have the following XAML code:

<Viewport3D.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Cut"/>
            </ContextMenu>
</Viewport3D.ContextMenu>

When I click my Viewport3D I get the context menu, how do I make it visible only when I touch a Model3D object, which is placed on my Viewport3D element. (I have several Model3D objects, and what I need is to get the context menu only when some of them is getting mouse click)

So to get over this issue I added a ContextMenuOpening event handler

<Viewport3D  
        ContextMenuOpening="ContextMenuOpen"

And the code behind

private void ContextMenuOpen(object sender, ContextMenuEventArgs e)
    {
        Point location = Mouse.GetPosition(myViewport);
        HitTestResult hit = VisualTreeHelper.HitTest(myViewport, location);
        var meshit = hit as RayMeshGeometry3DHitTestResult;
        SurfaceInfo inf = model3D.FindInfo(meshit.ModelHit);
        if (inf == null) // if Model3D object weren't touched 
        {
            e.Handled = true;
        }
    }

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