解散所有弹出窗口

时间:2011-12-19 23:43:25

标签: c# wpf xaml

如果我单击show popups并单击屏幕上的某个位置,它只会关闭一个弹出窗口。我怎么能让它解雇所有这些?

<Window x:Class="PopupTesting.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="1000" Width="1000">
<Grid>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Vertical">
        <Popup Name="myPopup1" Height="50" Width="100" Placement="Right">
            <Label Name="myLabel1" Content="This is a popup1!" Background="AliceBlue" Foreground="Blue"/>
        </Popup>

        <Popup Name="myPopup2" Height="50" Width="100" Placement="Left">
            <Label Name="myLabel2" Content="This is a popup2!" Background="Orange" Foreground="Blue"/>
        </Popup>

        <Button Content="Show popups" Height="43" Name="button1" Width="116" Click="button1_Click" />
        <Button Content="Show window" Height="43" Name="button2" Width="116" Click="button2_Click" />                                    
    </StackPanel>                
</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private bool _allOpened;

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (!_allOpened)
        {               
            myPopup1.StaysOpen = false;
            myPopup2.StaysOpen = false;


            myPopup2.IsOpen = true;
            myPopup1.IsOpen = true;

            _allOpened = true;
        }
        else
        {               
            myPopup1.IsOpen = false;
            myPopup2.IsOpen = false;
            _allOpened = false;
        }

    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        Window newWindow = new Window();
        newWindow.Owner = this;        
        newWindow.Show();
    }
}

0 个答案:

没有答案