[WPF] Grid Mouse events only firing on 3D object when on UserControl
Hi,
First of all, I'm very new to WPF so excuse me if this is a trivial question... But I cannot figure out what I'm doing wrong.
I had the following XAML layout on my Window:
xml Code:
<Grid MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" MouseUp="Grid_MouseUp" MouseWheel="Grid_MouseWheel">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button x:Name="button" Grid.Row="0" Content="Reset" />
<Viewport3D x:Name="viewport" Grid.Row="1">
<!-- (omitted) -->
</Viewport3D>
</Grid>
So just a Grid with a Button at the top and Viewport3D at the bottom.
I am then handling some Mouse events of the Grid in order to rotate the camera in my Viewport3D around the objects it is showing. I handle the events of the Grid rather than the Viewport, because if I handle the events on the Viewport, it seems they are only raised when the mouse is actually on the object (a cube in my case), not when it is in the 'void' beyond the object.
Anyway, since I am going to be using this setup more often, I decided to put the Viewport, and all the math in the mouse events that handles the rotation, in a new UserControl so I can re-use it more easily. So that's what I did: I created a new UserControl, pasted the Viewport into the (existing) Grid, and handle the Mouse events of the Grid again:
xml Code:
<UserControl x:Class="RushLevelEditor.ViewportCamera3D"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" MouseUp="Grid_MouseUp" MouseWheel="Grid_MouseWheel">
<Viewport3D Name="viewport" Grid.Row="0">
<!-- (omitted) -->
</Viewport3D>
</Grid>
</UserControl>
As far as I can see, this is the same. I just moved the Viewport3D to a UserControl and handle the mouse events there instead. I am still handling the mouse events of the grid rather than the Viewport3D for the same reason I mentioned earlier.
Anyway, it doesn't work. When I drop this UserControl on my Window and run, I can move the camera only when the mouse is on the cube in the Viewport, not when it is in the void beyond it. This puzzles me... It's the same behavior as if I was handling the mouse events of the Viewport, but I'm not!
How come the mouse events are raised when I don't click on the cube in the first scenario (Viewport3D directly in the Grid on the Window), but not in the second scenario (Viewport3D in the Grid on a UserControl)..?
Thanks for any help!
Re: [WPF] Grid Mouse events only firing on 3D object when on UserControl
I had this problem. Fixed it by moving the mouse event handlers to the root user control element and adding a background attribute to the user control.