Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Close WPF window that is running within another WPF's grid.

$
0
0

Hello,

I have a Wpf window(MainWindow) with 1 grid (Grid1) that contains 1 button(button that when clicked launches another WPF in Grid2) and a second grid (Grid2).

The second window (Window1) is launched inside Grid2 of the MainWindow.

Inside Window1 there are 3 buttons.

Button1 starts a messagebox (works fine)
Button2 changes the contents of the Label (works fine)
Button3 should close down the Window1 but doesn't.

My question is. What is the best way under these circumstances to close down Window1 and leave MainWindow open?



MainWindow.xaml

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfInsideWpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"><Grid x:Name="Grid1" Background="#FFCCD424"><Grid x:Name="Grid2" HorizontalAlignment="Left" Height="234" Margin="43,51,0,0" VerticalAlignment="Top" Width="438" Background="#FFDC1B1B"/><Button x:Name="button" Content="Run" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="43,10,0,0"/></Grid></Window>
MainWindow.xaml.vb
Class MainWindow
    Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
        Dim PO = New Window1()

        Dim Content As Object = PO.Content

        PO.Content = Nothing

        Grid2.Children.Add(Content)
    End Sub
End Class

Window1.xaml
<Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfInsideWpf"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300"><Grid Background="#FF40AE45"><Button x:Name="ButtonMessageBox" Content="Message Box" HorizontalAlignment="Left" Margin="24,27,0,0" VerticalAlignment="Top" Width="92"/><Button x:Name="ButtonChangeLabel" Content="Change Label" HorizontalAlignment="Left" Margin="24,70,0,0" VerticalAlignment="Top" Width="92"/><Label x:Name="label1" Content="" HorizontalAlignment="Left" Height="22" Margin="157,70,0,0" VerticalAlignment="Top" Width="103" Background="#FF9C4E4E"/><Button x:Name="ButtonExit" Content="Exit" HorizontalAlignment="Left" Margin="24,112,0,0" VerticalAlignment="Top" Width="92"/></Grid></Window>

Window1.xaml.vb
Public Class Window1

    Private Sub ButtonMessageBox_Click(sender As Object, e As RoutedEventArgs) Handles ButtonMessageBox.Click
        MessageBox.Show("Hello World")
    End Sub

    Private Sub ButtonChangeLabel_Click(sender As Object, e As RoutedEventArgs) Handles ButtonChangeLabel.Click

        If label1.Content = "" Then
            label1.Content = "Hello World"
            Return
        End If

        If label1.Content = "Hello World" Then
            label1.Content = ""
            Return
        End If
    End Sub



    Private Sub ButtonExit_Click(sender As Object, e As RoutedEventArgs) Handles ButtonExit.Click
        'Exit Programe
        Dim ExitMessage As Integer
        ExitMessage = MsgBox("Do you want to quit?", 1)
        If ExitMessage = 1 Then


            Me.Close()

        End If
        Return
    End Sub


End Class



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>