Viewport3D mouse event does not fire when background hits

I installed Viewport3D using MouseEventHandler

[...]
Main3DWindow.MouseUp += new MouseButtonEventHandler(mainViewport_MouseUp);
[...]
void mainViewport_MouseUp (object sender, MouseButtonEventArgs e) {
    Point location = e.GetPosition(Main3DWindow);
    ModelVisual3D result = GetHitTestResult(location);
    if (result == null) {
        _CurrentData.Unselect();
        return;
    }
    _CurrentData.SelectItemFromObjectList(result);
}

And it works great when you click on an object. My expectation was: If no object clicked (because the user clicked in the background), the result will be zero. But in fact, the mainViewport_MouseUp method is not even called.

My question is: how can I detect clicks in the background of Viewport3D?

+3
source share
1 answer

As you wrote, it will not be launched.

I decided that by defining events on the border and putting the viewport on the border. Sample from XAML:

<Border
                MouseWheel="mainViewport_MouseWheel"
                MouseMove="mainViewport_MouseMove"
                MouseLeftButtonDown="mainViewport_MouseLeftButtonDown"
                Background="Black">
                <Viewport3D
                    Name="mainViewport"
                    ClipToBounds="True"
                    Grid.Row="0"
                    Grid.Column="0"
                    Grid.ColumnSpan="3"
                    Margin="0,0,0,0">
.....
                </Viewport3D>
            </Border>

And in the code:

private void mainViewport_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Point location = e.GetPosition(mainViewport);
    try
    {
                ModelVisual3D result = (ModelVisual3D)GetHitTestResult(location);
        //some code.......
    }
    catch
    {
        //some code .......
    }
}
+2
source

All Articles