25

我有一个 WPFPopup控件,其中包含一些编辑控件(列表框、文本框、复选框),布局有相当多的空白。

Popup.StaysOpen设置为False,这是必需的。如果用户单击应用程序中的其他位置,则应认为编辑操作已中止并且弹出窗口应关闭。

不幸的是,每当用户在弹出窗口的背景区域(编辑控件之间的空间)内单击时,弹出窗口也会关闭。

我尝试将弹出窗口设置为Focusable. 我还尝试将弹出窗口的子项 (a Border) 设置为可聚焦。两方面都没有运气。

此外,鼠标事件似乎在弹出窗口中穿行。当我单击它时,弹出窗口下方的任何元素都会成为焦点。尽管PopupBorder(我点击进入)都具有IsHitTestVisibleFocusable设置为true

4

5 回答 5

40

最后,我发现以下工作。鉴于...

<Popup x:Name="_popup"
       StaysOpen="False"
       PopupAnimation="Slide"
       AllowsTransparency="True">

...我在调用后在构造函数中使用了此代码InitializeComponent...

// Ensure that any mouse event that gets through to the
// popup is considered handled, otherwise the popup would close
_popup.MouseDown += (s, e) => e.Handled = true;
于 2009-03-06T20:46:24.860 回答
7

它会忽略 Popup 和 Border 上的 Focusable 似乎很奇怪。当鼠标悬停在边框上时,我可以通过在触发器中更改 StaysOpen 来解决您的问题:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ToggleButton x:Name="btnPop" Content="Pop!" Width="100" Height="50"/>
    <Popup Placement="Bottom" PlacementTarget="{Binding ElementName=btnPop}" IsOpen="{Binding IsChecked, ElementName=btnPop}">
        <Popup.Style>
            <Style TargetType="{x:Type Popup}">
                <Setter Property="StaysOpen" Value="False"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsMouseOver, ElementName=brd}" Value="True">
                        <Setter Property="StaysOpen" Value="True"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Popup.Style>
        <Border x:Name="brd" Background="White" BorderThickness="1" BorderBrush="Black">
            <StackPanel>
                <TextBox Margin="10"/>
                <TextBlock Text="Some text is here." Margin="10"/>
                <TextBox Margin="10"/>
            </StackPanel>            
        </Border>
    </Popup>
</Grid>
于 2009-03-06T20:48:01.610 回答
1

我最好的猜测是你有一些透明度问题。尝试在弹出窗口上设置背景画笔。

于 2009-03-06T20:09:21.407 回答
1

您没有将 Popup 嵌套在 ToggleButton 或其他类型的 Button 中吗?然后在 Popup 级别停止路由事件将是合乎逻辑的。

于 2009-07-13T09:19:54.977 回答
-1

可以设置StayOpen =true,并设置一个定时器,在Popup的MouseLeave事件timer.Start()中,比如3秒后关闭这个popup,在MouseEnter事件中,timer.Stop()。它会起作用。

于 2013-07-17T06:40:24.090 回答