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()
Dim PO As New PO()
content1 = PO.Content
PO.Content = Nothing
content1.Name = "hello"
PO.textBox.Text = "hello"
PO.label.Content = "Some Text " & "hello"
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] End Sub
End Class
The c# code looks like this.
[code]
public MainWindow()
{
InitializeComponent();
}
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
//'Add PO to Grid1
{
//'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";
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.

Afzaal Ahmad ZeeshanRank #60 · 27.6K reputationPosted Jan 17, 2017, 12:59 AM EST
Tony Allan66 reputationPosted Jan 17, 2017, 9:05 AM EST
The c# code won’t compile (2 errors).
Line “content1.Name = "hello";” has error.
'object' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Line “Grid1.Children.Add(content1);” has error.
Argument 1: cannot convert from 'object' to 'System.Windows.UIElement'
Amit GuptaRank #190 · 12.8K reputationPosted Jan 17, 2017, 10:41 AM EST
Tony Allan66 reputationPosted Jan 17, 2017, 12:23 PM EST
Thank you Amit.
But this then brings up an error in this line.
Tony Allan66 reputationPosted Jan 17, 2017, 7:53 PM EST
Again thank you Amit.
Amit GuptaRank #190 · 12.8K reputationPosted Jan 18, 2017, 2:32 AM EST
Tony Allan66 reputationPosted Jan 18, 2017, 10:04 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 18, 2017, 10:43 AM EST