使用概念发现here on StackOverflow。 请注意,ToggleButton.IsHitTestVisible
绑定到Popup.IsOpen
,与StaysOpen="False"
。这应该意味着触摸任何地方外的Popup
会导致它关闭。但是...WPF弹出ISOPEN问题
按照预期,在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
派生的自定义控制内。在我提出这个问题的时候,这似乎并不重要,对不起。
感谢您的建议。然而,这不是正确的,你不可能帮助不幸。经过几个小时的调试后,结果是因为它在一个自定义控件的'DataTemplate'中。这个控件是从'ListBox'派生出来的,并且导致了奇怪的行为。我修改了它,它按预期工作。 – erodewald 2013-04-29 15:50:55
我很高兴你的上帝,它仍然工作 – 2013-04-29 16:26:01