Michael Matejko
Michael Matejko

Reputation: 163

Close WPF popup when user clicks on it

How do I close the following popup when its content is clicked?

<Button Name="myButton" Content="Hover to open" />
<Popup PlacementTarget="{Binding ElementName=myButton}" Placement="Bottom">
    <Popup.Resources>
        <DataConversion:BooleanOrConverter x:Key="booleanOrConverter" />
    </Popup.Resources>            
    <Popup.IsOpen>
        <MultiBinding Mode="OneWay" Converter="{StaticResource booleanOrConverter}">
            <Binding Mode="OneWay" ElementName="myButton" Path="IsMouseOver"/>
            <Binding RelativeSource="{RelativeSource Self}" Path="IsMouseOver" />
        </MultiBinding>
    </Popup.IsOpen>
    <!-- some content here -->
</Popup>

I wanted to avoid writing code in the code behind file and binding visual and behavioral things like IsOpen property or MouseDown event to the ViewModel.

Upvotes: 3

Views: 6007

Answers (2)

Tim Rogers
Tim Rogers

Reputation: 21713

You need an EventTrigger on your Popup that triggers a storyboard that contains a BooleanAnimationUsingKeyFrames that sets IsOpen to false, similar to this. Easier to just use code-behind ;o)

Upvotes: 2

brunnerh
brunnerh

Reputation: 184516

You could just make the Popup.Child a Button (style it to not look like one), handle its Click event and set IsOpen to false (preferably using SetCurrentValue to not destroy the binding).

You can either do this in code behind or using a behavior/trigger action.

Upvotes: 2

Related Questions