WPF Popup IsOpen issue

喜你入骨 提交于 2019-12-10 18:02:25

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/16245172/wpf-popup-isopen-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!