Tony Allan

Tony Allan

  • NA
  • 71
  • 10.1k

How to launch a WPF window inside a Grid.

Jan 17 2017 3:29 AM
Hello.
I have a Solution that contains 2 Classes.
1- MainWindow (which contains a button and a grid).
2- PO (which contains a textbox and a label).
 
When the solution is launched the MainWindow is displayed with button and grid.
When the button is clicked PO is launched and appears the the grid of MainWindow.
 
All works fine with my VB version but when I change it to C# I get errors.
 
The VB code looks like this. 
 [code]
Class MainWindow
    Private Sub Window_Initialized_1(sender As Object, e As EventArgs)
    End Sub
    Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
        ''Add PO to Grid1
        Dim content1 As Object
        Dim PO As New PO()
        content1 = PO.Content
        PO.Content = Nothing
        content1.Name = "hello"
        PO.textBox.Text = "hello"
        PO.label.Content = "Some Text " & "hello"
        Grid1.Children.Add(content1)
    End Sub
End Class
[/code] 
 
 The c# code looks like this.
[code] 
 
public MainWindow()
        {
            InitializeComponent();
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            //'Add PO to Grid1

            {
                object content1 = null;
                PO PO = new PO();
                content1 = PO.Content;
                PO.Content = null;
                content1.Name = "hello";
                PO.textBox.Text = "hello";
                PO.label.Content = "Some Text- " + "hello";
                Grid1.Children.Add(content1);
            }
        }
    }
[/code].
 
What am I doing wrong?
 
I have included both solution files.
 AddWPFWindowToGrid - which is the VB version.
and
AddWPFWindowToGridInC - which is the C# version. 
 
 Thank you.
 
 
 

Attachment: AddWPF.zip

Answers (8)