2

I have the below code:

<ToggleButton x:Name="toggleBtn" Content="Open Popup" /> <Popup x:Name="pp" Placement="Bottom" StaysOpen="False" IsOpen="{Binding ElementName=toggleBtn, Path=IsChecked, Mode=TwoWay}"> <Border Background="LightBlue" BorderBrush="Black" BorderThickness="1" Padding="10"> <TextBlock Text="Popup content" /> </Border> </Popup> 

Current behavior: When the popup is open and I click on the ToggleButton again, the popup reopens. I don’t understand why this happens.

Expected behavior: When the popup is open and I click on the ToggleButton again, the popup should not reopen — similar to how a ComboBox behaves.

I’ve tried handling this in code-behind without binding (using the Checked and Unchecked events), and I’ve also tried handling it with the Popup.Closed event, but neither approach worked.

1 Answer 1

6

The popup does not reopen. It closes first as a result of one action and then opens again as a result of another.

This is what happens when you click the ToggleButton while the popup is open:

Event 1: MouseDown outside of the popup:
The popup handles the event because of StaysOpen="False" and begins closing.
pp.IsOpen is set to false.
TwoWay Binding sets toggleBtn.IsChecked=false .

Event 2: MouseUp over toggle button:
Nothing happens. Click event will be emitted.

Event 3: Click on toggle button:
toggleBtn.IsChecked is changed from false to true.
TwoWay Binding sets pp.IsOpen=true

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.