How can I hide the popup, when mouse click on anywhere outside using wpf?

Total Post:88

Points:616
WPF 
WPF
 10108  View(s)
Ratings:
Rate this:
Hi developers!

I got my UserControl that contain:

    Button

    Popup (contain Text block)

.XAML file 

<UserControl>

<button Name="btnShowPopup" Content="Button" Click=" btnShowPopup _Click"/>

<Popup Name="popup" StaysOpen="true">

<TextBlock Text="Popup"/>

</Popup>

</UserControl> 

Code Behide 

private void btnShowPopup _Click(object sender, System.Windows.RoutedEventArgs e)

{

   this.popup.IsOpen=!this.popup.IsOpen;

}

QUESTION: I want to hide the popup, when mouse click on anywhere outside the btnShowPopup button.

please help me. thank in advance!

  1. Post:105

    Points:735
    Re: How can I hide the popup, when mouse click on anywhere outside using wpf?

    Hi!

    You can use to ToggleButton instead of button it would easy.

    <ToggleButton x:Name="myToggleButton" />
    <Popup x:Name="Popup"
        IsOpen="{Binding Path=IsChecked, ElementName= myToggleButton }"
        Placement="Right"
        PlacementTarget="{Binding ElementName= myToggleButton }"
        AllowsTransparency="True"
        Focusable="False"
        PopupAnimation="Fade"
        StaysOpen="False">
        <Textblock Text="Write Here content" />
    </Popup>

      Modified On Mar-30-2018 11:32:14 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!