You have a number of problems. The first and easiest thing is that you open and do not close ToolTip . You said that I want the tooltip to show for the same amount of time the button is clicked, and this is easy to implement using the PreviewMouseDown and PreviewMouseUp events:
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e) { m_toolTip.PlacementTarget = PlacementTarget; m_toolTip.IsOpen = true; } private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e) { m_toolTip.IsOpen = false; }
...
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Button PreviewMouseDown="Button_PreviewMouseDown" PreviewMouseUp="Button_PreviewMouseUp">Show ToolTip</Button> <StatusBar Grid.Row="2"> <StatusBarItem> <TextBlock Name="PlacementTarget" Text="TextBlock With ToolTip"> <TextBlock.ToolTip> <ToolTip x:Name="m_toolTip" Placement="Top" HorizontalOffset="50" VerticalOffset="-5">ToolTip</ToolTip> </TextBlock.ToolTip> </TextBlock> </StatusBarItem> </StatusBar> </Grid>
Your other problem is a little more difficult to fix ... it seems to me that there may be an error related to the positioning of your ToolTip . Usually, despite what @icebat said, you can reposition the ToolTip using the ToolTip.Placement property . This can be set to one of the PlacementMode Enumerations .
The default value is Mouse , and this is the definition from the linked page on MSDN:
Place a Popup control that aligns its top edge with the bottom edge of the mouse's bounding box and aligns the left edge with the left edge of the bounding box of the mouse. If the bottom edge of the screen hides the popup, it is rebuilt to align it with the top edge of the mouse bounding box. If the top edge of the screen hides the popup, the control rebuilds itself to align with the top edge of the screen.
This explains why the ToolTip displayed far from the TextBlock placement target ... because the Button and therefore the mouse (when clicked) is far from the TextBlock . However, setting the Placement property to a different value requires a wide range of positions to be achieved. However, by setting different values ββfor the Placement property, only the ToolTip will be displayed in the upper left corner of the screen.
To resolve this situation, you should also set the ToolTip.PlacementTarget Property , as @icebat was correctly noted in the comment, but apparently only from the code. After the PlacementTarget property is set, the value of the Placement property works as expected. On this page:
You can place a hint by setting the properties of PlacementTarget, PlacementRectangle, Placement, HorizontalOffset and VerticalOffsetProperty.
