0

I have seen a lot of debate about when to use tooltip and when to use popup but I don't know which one is better for my case.

I have a button. When I click on it, the popup panel will appear and it has a lot of text and a small image (so it will be a quite big panel). The panel must stay there until I move my cursor OFF THE BUTTON (it must still close when the cursor is still on the panel but off the button).

<Button Click="clicked" MouseLeave="mouseleaved"/>
<Popup Name="mypopup">
    <stuff>
</Popup>

private void clicked(object sender, RoutedEventArgs e) {
    mypopopup.isopen = true;
}

private void mouseleaved(object sender, MouseEventArgs e) {
    mypopup.isopen = false;
}

This is where I got to so far. The problem is that sometimes, the Popup appears on top of the button (which blocks the view of the button and so MouseLeave event kicks off, and Popup instantly disappears). I want the Popup to stay until i move the cursor away off the button.

So I did some google, and I think Tooltip may avoid this problem. But how to get Tooltip to appear on button click and not button hover?

Which one is better for me? Tooltip or Popup?

EDIT

I think I was not too clear with my question. I am asking which one i should use- Tooltip vs Popup based on MY SPECIFIC SITUATION (paragraph 2) and not in general. I think Popup is the right one to use but I have problems with using it (paragraph 3). so my question is can I solve this problem with Popup or should I use Tooltip better for this?

Mad Program
  • 129
  • 1
  • 1
  • 9
  • Possible duplicate of [How to show tooltip in code behind in WPF](https://stackoverflow.com/questions/23041768/how-to-show-tooltip-in-code-behind-in-wpf) – dymanoid May 24 '17 at 09:18
  • This one in similar as well https://stackoverflow.com/questions/11073470/tooltip-versus-popup-wpf-control?rq=1 – uli78 May 24 '17 at 09:19
  • It's similar question, but both do not answer my question. I am asking based on my specific situation (see paragraph 2). The third paragraph I describe the problem when I use Popup. So my question is- is there a way I can solve this problem when using Popup or is it better to use Tooltip – Mad Program May 24 '17 at 11:19

1 Answers1

2

But how to get Tooltip to appear on button click and not button hover?

Handle the Click event for the Button and set the IsOpen property of the Popup to true:

private void Button_Click(object sender, RoutedEventArgs e)
{
    popup1.IsOpen = true;
}

<Popup x:Name="popup1" StaysOpen="False">
    <TextBlock>popup content...</TextBlock>
</Popup>
<Button Click="Button_Click" Content="op" />

Which one is better for me? Tooltip or Popup?

Popup is preferable whenever you want to customize the behaviour in any way.

Edit: If I understand your issue correctly, this should work:

<Button x:Name="button" Content="Button" Click="clicked" MouseLeave="mouseleaved"/>
<Popup Name="popup" PlacementTarget="{Binding ElementName=button}" StaysOpen="True" MouseLeave="mouseleaved">
    <Border Background="Yellow">
        <TextBlock>contents...</TextBlock>
    </Border>
</Popup>

private void clicked(object sender, RoutedEventArgs e)
{
    popup.IsOpen = true;
}

private void mouseleaved(object sender, MouseEventArgs e)
{
    if (!button.IsMouseOver && !popup.IsMouseOver)
        popup.IsOpen = false;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Your answer makes sense. But does not fully answer my question. sorry if i don't think i made it too clear. please see my edit at the top. – Mad Program May 24 '17 at 11:28
  • As mentioned Popup is the right choice. If you have any issues with it you should provide a repo of your issue: https://stackoverflow.com/help/how-to-ask – mm8 May 24 '17 at 11:52
  • I open the popup when button clicked. I close the popup using MouseLeave event on button (that is, when i move the mouse off the button). Sometimes the popup opens on top of the button so MouseLeave is called instantly (because it is covering over the button). How do I resolve this problem if using Popup? – Mad Program May 24 '17 at 12:14
  • I repeat myself here: Post some code that demonstrates this behaviour if you want any help. – mm8 May 24 '17 at 12:15
  • But you could position a Popup by using the Placement, PlacementRectangle, and Offset properties: https://msdn.microsoft.com/en-us/library/ms753224(v=vs.85).aspx. Does that answer your question? – mm8 May 24 '17 at 12:24
  • I thought about that too, but I prefer the button to stay where it is now. I have a few buttons, each that open a different panel and I want them all open at the same place. I don't care if the buttons get blocked by the Popup. The problem is that the Popup disappears immediately after I click the button because MouseLeave event is fired off. How can I prevent the Popup from firing the MouseLeave event on the button? – Mad Program May 24 '17 at 12:41
  • So your issue is that the MouseLeave event fires when you hover the Popup? – mm8 May 24 '17 at 12:55
  • Thanks. that is close to what I want now. When the mouse cursor is ON the Popup but OFF the button, I still want the Popup to disappear. (whether cursor is on Popup or not does not matter) – Mad Program May 24 '17 at 13:54
  • Remove the && !popup.IsMouseOver condition then? – mm8 May 24 '17 at 13:58