使用概念發現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