HOW CAN I HIDE THE POPUP, WHEN MOUSE CLICK ON ANYWHERE OUTSIDE USING WPF?

jacob rasel

Total Post:88

Points:616
Posted by  jacob rasel
WPF 
WPF
 9103  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. shreesh chandra shukla

    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>

Answer

NEWSLETTER

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