4

使用在 StackOverflow 上找到的概念。请注意,ToggleButton.IsHitTestVisible绑定到Popup.IsOpen,与StaysOpen="False"这应该意味着触摸外面的任何地方Popup都会导致它关闭。然而...

触摸/单击ListBoxItem中的ItemsControl不会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. 在我提出这个问题时,它似乎并不相关,抱歉。

4

2 回答 2

1

我认为在您的情况下,问题出在弹出窗口的位置或大小上。在尝试您的代码时它确实有效,但是我必须Placement="Center"在弹出窗口上设置并设置弹出窗口内网格的大小。

没有前者,弹出窗口不会放在里面,而没有后者,弹出窗口的大小只是其内容的大小(意味着没有外部可以点击)。

尝试首先将弹出窗口的背景设置为红色或其他内容,以查看弹出窗口的实际位置和大小是否正确。

于 2013-04-26T21:33:07.377 回答
1

事实证明这是因为它位于从 ListBox 派生的自定义控件中。在我提出这个问题时,它似乎并不相关,抱歉。

于 2013-04-29T15:53:16.150 回答