none
WPF中popup点击空白处如何自动关闭呢?(谢谢) RRS feed

答案

全部回复

  • 您好 Marry_Xia,

    >>"WPF中popup点击空白处如何自动关闭?"

    我们可以注册popup的MouseDown事件,在事件的方法中将Popup的IsOpen属性设置为false。

    private void Popup1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Popup1.IsOpen = false;
    }

    另外我们还可以在Popup的样式中使用EventTrigger来实现。以下代码供您参考。

    <Popup Name="Popup1"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="200" >
        <Popup.Style>
            <Style TargetType="{x:Type Popup}">
                <Style.Triggers>
                    <EventTrigger RoutedEvent="MouseDown">
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Duration="0:0:0.100" Storyboard.TargetProperty="IsOpen">
                                    <BooleanKeyFrameCollection>
                                        <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0.100"/>
                                    </BooleanKeyFrameCollection>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </Popup.Style>
        <TextBlock Background="LightBlue" >   
            This is popup text
        </TextBlock>
    </Popup>
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月17日 4:27
  • 非常感谢您的回复,我试过了,不行呀!就是在其他地方点击还是没关闭呢。
    2016年5月19日 10:07
  • 您好 Marry_Xia,

    很抱歉误解了您的问题,如果你希望点击任意一点就可以关闭Popup,建议您注册Window的MouseDown事件。

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Popup1.IsOpen = false;
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月26日 7:56
  • 谢谢您在百忙之中帮我解答疑难问题!已经OK了,这里用到了路由事件!我明白了。
    • 已标记为答案 Marry_qing 2016年5月27日 7:02
    2016年5月27日 7:02