Why does a WPF Popup close when its background area is clicked?

前端 未结 5 1108
温柔的废话
温柔的废话 2020-12-14 01:47

I have a WPF Popup control that contains some editing controls (list boxes, text boxes, check boxes) laid out with quite a bit of whitespace.

Popu

相关标签:
5条回答
  • 2020-12-14 02:30

    Don't you have your Popup nested in a ToggleButton or other kind of Button? Then stopping the routed event at Popup level would be logical to get working.

    0 讨论(0)
  • 2020-12-14 02:34

    My best guess is you have some transparency issues going on. Try setting a background brush on the popup.

    0 讨论(0)
  • 2020-12-14 02:40

    you can set StayOpen=true,and set a timer,in the Popup's MouseLeave event timer.Start(),such as after 3 seconds,close this popup,in the MouseEnter event,timer.Stop(). It will works.

    0 讨论(0)
  • 2020-12-14 02:46

    In the end, I found that the following worked. Given...

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

    ...I used this code in the constructor after calling 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;
    
    0 讨论(0)
  • 2020-12-14 02:53

    It does seem odd that it would ignore Focusable on the Popup and Border. I was able to fix your problem by changing StaysOpen in a trigger when the mouse is over the Border:

    <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>
    
    0 讨论(0)
提交回复
热议问题