使用在StackOverflow上找到的一个概念。请注意,ToggleButton.IsHitTestVisible绑定到Popup.IsOpen,并且StaysOpen="False"。这意味着触摸Popup之外的任何地方都会导致它关闭。然而... 在ItemsControl中触摸/点击ListBoxItem不会关闭Popup,这是有意的。在Popup内部的任何其他地方触摸都会关闭它。根据设置,这似乎不合理。 <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> 有什么想法吗?谢谢。 编辑:已解决 事实证明,这是因为它在自定义控件内发生,该控件派生自ListBox。当我提出这个问题时,它似乎不相关,抱歉。
我认为在你的情况下,问题要么是弹出窗口的位置或大小。在尝试您的代码时,它确实起作用,但我必须在Popup上设置Placement="Center"并设置弹出窗口内部网格的大小。如果没有前者,则弹出窗口不会放置在其中,而如果没有后者,则弹出窗口的大小仅为其内容的大小(这意味着没有外部可供点击)。首先将Popup的背景设置为红色或其他颜色,以查看弹出窗口是否实际定位和调整大小。