4

I set up a Viewport3D with a 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 pretty fine when an object is clicked. My expectation was: If no object is clicked (because the user clicked at the background) the result is null. But in fact the mainViewport_MouseUp-method is not even called.

My question: how can i detect clicks on the background of the Viewport3D?

xelor
  • 319
  • 3
  • 10

1 Answers1

3

It is as you wrote, it wont be fired.

I solved that by defining events on border and put viewport into border. Sample is 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 .......
    }
}
het2cz
  • 166
  • 4
  • Did this solution work in your project? I have the same problem as before - exept when clicking exact on the border of the border-uiElement – xelor Feb 13 '14 at 14:09
  • yes it works and results is null when I click in empty area of viewport. BORDER is inside a GRID but it works even if there is no grid at all. – het2cz Feb 13 '14 at 14:31
  • here is a sample project uploaded to googledrive. Can you try it? [link]https://drive.google.com/file/d/0B_gJl-kX9L6cY1BZak5ueEh1dWc/edit?usp=sharing – het2cz Feb 13 '14 at 15:11
  • your solution is actual working! it will be not a big deal to modify my own project in that way. thank you very much for your work and time!! – xelor Feb 14 '14 at 10:29
  • 3
    just comment for others ... its importent to set a background to the border! otherwise the mouseclick-event will only be fired when the border of the border is hit. – xelor Feb 17 '14 at 06:48