erodewald
erodewald

Reputation: 1825

WPF Popup IsOpen issue

Using a concept found here on StackOverflow. Note that the ToggleButton.IsHitTestVisible is bound to Popup.IsOpen, with StaysOpen="False". This should mean that touching anywhere outside the Popup would cause it to close. However...

Touching/Clicking on an ListBoxItem in the ItemsControl won't close the Popup, as is intended. Touching anywhere else within the Popup does close it. That doesn't seem to add up, according to how this is set up.

<Grid ClipToBounds="True">
    <Border Name="Root">
        <ToggleButton x:Name="PART_Toggle"
                  ClickMode="Release"
                  IsHitTestVisible="{Binding ElementName=PART_Popup,
                                             Path=IsOpen,
                                             Mode=OneWay,
                                             Converter={StaticResource BooleanInverter}}"/>
    </Border>
    <Popup x:Name="PART_Popup"
           IsOpen="{Binding ElementName=PART_Toggle,
                            Path=IsChecked}"
           PlacementTarget="{Binding ElementName=PART_Toggle}"
           StaysOpen="False">
        <Grid Background="Transparent">
            <Grid>
                <!-- Anything here (outside of the Item) -->
                <ItemsControl>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <!-- Anything in this item template works. The popup does not close -->
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>
            </Border>
        </Grid>
    </Popup>
</Grid>

Any ideas? Thanks.


Edit: Solved

Turns out this was happening because it was inside a custom control which was derived from ListBox. It didn't seem relevant at the time I made this question, sorry.

Upvotes: 4

Views: 3034

Answers (2)

erodewald
erodewald

Reputation: 1825

Turns out this was happening because it was inside a custom control which was derived from ListBox. It didn't seem relevant at the time I made this question, sorry.

Upvotes: 1

Darko Kenda
Darko Kenda

Reputation: 4960

I think in your case the problem is either the position or size of the popup. When trying your code it did work, however I had to set Placement="Center" on the Popup and set the size of the grid inside the popup.

Without the former, the popup was not placed inside the while without the latter the popup's size was just that of its content (meaning there was no outside to click).

Try first setting the Popup's background to Red or something to see if the popup is actually positioned and sized correctly.

Upvotes: 1

Related Questions