如何关闭WPF弹出窗口?

时间:2012-08-18 12:39:24

标签: c# wpf

我正在WPF中开发项目,我在项目中使用弹出窗口时遇到问题。我在窗口中使用弹出控件,如下所示: -

<Popup HorizontalAlignment="Center" VerticalAlignment="Center" 
    AllowsTransparency="True" x:Name="popup" Placement="Center" 
    OpacityMask="#FFC86E6E" Closed="popup_Closed" >
    <Grid Height="auto" Width="auto" Margin="0" >
        <Grid.RowDefinitions>
            <RowDefinition Height="0.488*"/>
            <RowDefinition Height="0.512*"/>
        </Grid.RowDefinitions>

        <Frame x:Name="popupframe" Margin="0" Grid.Row="1"  />
        <Button Width="30" Height="30"  HorizontalAlignment="Right" 
                            Margin="0,0,10,-50" VerticalAlignment="Center" 
                            BorderThickness="0" BorderBrush="{x:Null}" 
                            ClickMode="Press" Click="Button_Click" 
                            Foreground="{x:Null}">
            <Button.Background>
                <ImageBrush ImageSource="Image/1329666144_button_cancel.png" Stretch="UniformToFill"/>
            </Button.Background>
        </Button>
            </Grid>
</Popup>

现在我使用文本框和按钮在wpf中创建新页面,并将此页面设置为下面的弹出框架: -

popupframe.Content=new SessionObjection();

现在我想用页面按钮关闭弹出窗口。我怎么做......

3 个答案:

答案 0 :(得分:8)

您可以将IsOpen属性设置为false来关闭弹出窗口。

答案 1 :(得分:4)

你可以试试

private void btnClosePopup_Click(object sender, RoutedEventArgs e)
        {
            popup.IsOpen = false;
        }

答案 2 :(得分:0)

如果您想在XAML代码中解决它,这是一个有效的解决方案(将关闭按钮放在弹出窗口中):

    <Button Name="CloseThisPopUp" VerticalAlignment="Top" HorizontalAlignment="Right" Content="X">
                                        <Button.Triggers>
                                            <EventTrigger RoutedEvent="Button.Click">
                                                <EventTrigger.Actions>
                                                    <BeginStoryboard>
                                                        <Storyboard>
                                                            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseThisPopUp" Storyboard.TargetProperty="IsOpen">
                                                                <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="False" />
                                                            </BooleanAnimationUsingKeyFrames>
                                                        </Storyboard>
                                                    </BeginStoryboard>
                                                </EventTrigger.Actions>
                                            </EventTrigger>
                                        </Button.Triggers>
                                    </Button>

您也可以使用此代码打开弹出窗口,只需将IsOpen属性更改为&#34; True&#34;。