Reputation: 322
I was testing a Popup control in WPF with the following code
<Window x:Class="Popup1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Popup1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="250">
<Grid>
<TextBlock TextWrapping="Wrap">You can use a popup to provide a link for a specific
<Run TextDecorations="Underline" MouseEnter="ContentElement_OnMouseEnter">
term
</Run>
</TextBlock>
<Popup Name="popLink" StaysOpen="False" Placement="Mouse" MaxWidth="200"
PopupAnimation="Slide" AllowsTransparency="True">
<Border>
<TextBlock Margin="10" TextWrapping="Wrap">
For more information, see
<Hyperlink NavigateUri="http://en.wikipedia.org/wiki/Term" Click="Hyperlink_OnClick">Wikipedia</Hyperlink>
</TextBlock>
</Border>
</Popup>
</Grid>
</Window>
and the handlers
private void ContentElement_OnMouseEnter(object sender, MouseEventArgs e) {
popLink.IsOpen = true;
}
private void Hyperlink_OnClick(object sender, RoutedEventArgs e) {
Process.Start(((Hyperlink) sender).NavigateUri.ToString());
}
The result is a trivial window that contains a textblock
with a link to a popup control that visually appears when the mouse hovers over the link to the popup.
The normal behavioris the popup to stay visible until a mouse click. This works fine as long as the mouse click is not on the link to the popup
The strange behaviorthat i can't explain happens when i click the mouse over the link to the popup.Then, the popup closes ( as expected ) but it never appears again when the mouse hovers over the link (as it should).
Can you explain this behavior?
Upvotes: 1
Views: 760
Reputation: 322
After some tweaking the best behaviour is achieved if we add an extra event (comparing to the initial code) handler for the Popup Close event that sets the IsOpen property to false when the popup closes
private void PopLink_OnClosed(object sender, EventArgs e) {
if (popLink.IsOpen) {
popLink.IsOpen = false;
}
}
and the ammenment in XAML
<Popup Name="popLink" StaysOpen="False" Placement="Mouse" MaxWidth="200"
PopupAnimation="Slide" AllowsTransparency="True"
Closed="PopLink_OnClosed">
<Border Background="Bisque">
<TextBlock Margin="10" TextWrapping="Wrap">
For more information, see
<Hyperlink NavigateUri="http://en.wikipedia.org/wiki/Term" Click="Hyperlink_OnClick">Wikipedia</Hyperlink>
</TextBlock>
</Border>
</Popup>
Upvotes: 0
Reputation: 13458
As commented, the reason is probably a race condition between closing popup and re-opening because the mouse is over the textblock. You can prevent this situation by delaying the popup open action until current work is completed:
private void ContentElement_OnMouseEnter(object sender, MouseEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() => popLink.IsOpen = true));
}
Regarding your title text: the MouseEnter event is actually fired (debug it!), just the action within is not working as expected because the popup is in an inconsistent state.
Upvotes: 2