使用发现的概念在 StackOverflow 上 https://stackoverflow.com/a/8946055/24399. 请注意,ToggleButton.IsHitTestVisible
一定会Popup.IsOpen
, with StaysOpen="False"
.这应该意味着触摸任何地方outside the Popup
会导致它关闭。然而...
触摸/点击ListBoxItem
in the ItemsControl
不会关闭Popup
,正如预期的那样。触摸范围内的任何其他地方Popup
does关闭它。根据它的设置方式,这似乎不合理。
<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
。抱歉,在我提出这个问题时,这似乎并不相关。
我认为在你的情况下问题是弹出窗口的位置或大小。当尝试你的代码时它确实有效,但是我必须设置Placement="Center"
在弹出窗口上并设置弹出窗口内网格的大小。
如果没有前者,弹出窗口不会放置在内部,而如果没有后者,弹出窗口的大小只是其内容的大小(意味着没有外部可单击)。
首先尝试将弹出窗口的背景设置为红色或其他颜色,以查看弹出窗口的实际位置和大小是否正确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)