Reputation: 2018
I've got a Grid
and within the grid I have child elements. The grid has a ToolTip
which I only want to show when the Mouse pointer is directly over the Grid
(and not over any of the child elements).
I've written a little XAML sample below to show the problem. It contains a Grid
(with 2 columns, 2 rows and a ToolTip
), and a Button
in the Top Left grid cell.
When I move the mouse pointer over the Button
it shows the ToolTip
. And when I move the mouse pointer over the Grid
it also shows the ToolTip
. I only want the ToolTip
to be displayed when the mouse is directly over the Grid
(and not over the Button
).
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<Grid ToolTip="I'm over the grid"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="LightSteelBlue">
<Grid.RowDefinitions >
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0"
Grid.Column="0"
Content="MY BUTTON"/>
</Grid>
</Window>
There is a IsMouseDirectlyOver property so I was wonder if there is any way I could use that to only show the ToolTip
when the mouse is directly over the Grid
.
Upvotes: 1
Views: 855
Reputation: 23270
Just detach that parent relationship and move it to an element that falls behind the other objects but as a child will still receive the MouseOver just for the grid and none of the other elements, one way would be like;
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<Grid HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="LightSteelBlue">
<Grid.RowDefinitions >
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle Grid.RowSpan="2" Grid.ColumnSpan="2"
Fill="Transparent"
ToolTip="I'm only shown when I have a mouse on me instead of all the children, because I'm special :)"/>
<Button Grid.Row="0"
Grid.Column="0"
Content="MY BUTTON"/>
</Grid>
</Window>
Hope this helps, cheers.
Upvotes: 1