// Create the CanvasmyParentCanvas =newCanvas();myParentCanvas.Width=400;myParentCanvas.Height=400;// Define child Canvas elementsmyCanvas1 =newCanvas();myCanvas1.Background=Brushes.Red;myCanvas1.Height=100;myCanvas1.Width=100;Canvas.SetTop(myCanvas1,0);Canvas.SetLeft(myCanvas1,0);myCanvas2 =newCanvas();myCanvas2.Background=Brushes.Green;myCanvas2.Height=100;myCanvas2.Width=100;Canvas.SetTop(myCanvas2,100);Canvas.SetLeft(myCanvas2,100);myCanvas3 =newCanvas();myCanvas3.Background=Brushes.Blue;myCanvas3.Height=100;myCanvas3.Width=100;Canvas.SetTop(myCanvas3,50);Canvas.SetLeft(myCanvas3,50);// Add child elements to the Canvas' Children collectionmyParentCanvas.Children.Add(myCanvas1);myParentCanvas.Children.Add(myCanvas2);myParentCanvas.Children.Add(myCanvas3);
// Create the Grid.grid1 =newGrid ();grid1.Background=Brushes.Gainsboro;grid1.HorizontalAlignment=HorizontalAlignment.Left;grid1.VerticalAlignment=VerticalAlignment.Top;grid1.ShowGridLines=true;grid1.Width=425;grid1.Height=165;// Define the Columns.colDef1 =newColumnDefinition();colDef1.Width=newGridLength(1,GridUnitType.Auto);colDef2 =newColumnDefinition();colDef2.Width=newGridLength(1,GridUnitType.Star);colDef3 =newColumnDefinition();colDef3.Width=newGridLength(1,GridUnitType.Star);colDef4 =newColumnDefinition();colDef4.Width=newGridLength(1,GridUnitType.Star);colDef5 =newColumnDefinition();colDef5.Width=newGridLength(1,GridUnitType.Star);grid1.ColumnDefinitions.Add(colDef1);grid1.ColumnDefinitions.Add(colDef2);grid1.ColumnDefinitions.Add(colDef3);grid1.ColumnDefinitions.Add(colDef4);grid1.ColumnDefinitions.Add(colDef5);// Define the Rows.rowDef1 =newRowDefinition();rowDef1.Height=newGridLength(1,GridUnitType.Auto);rowDef2 =newRowDefinition();rowDef2.Height=newGridLength(1,GridUnitType.Auto);rowDef3 =newRowDefinition();rowDef3.Height=newGridLength(1,GridUnitType.Star);rowDef4 =newRowDefinition();rowDef4.Height=newGridLength(1,GridUnitType.Auto);grid1.RowDefinitions.Add(rowDef1);grid1.RowDefinitions.Add(rowDef2);grid1.RowDefinitions.Add(rowDef3);grid1.RowDefinitions.Add(rowDef4);// Add the Image.img1 =newImage();img1.Source= runicon;Grid.SetRow(img1,0);Grid.SetColumn(img1,0);// Add the main application dialog.txt1 =newTextBlock();txt1.Text="Type the name of a program, folder, document, or Internet resource, and Windows will open it for you.";txt1.TextWrapping=TextWrapping.Wrap;Grid.SetColumnSpan(txt1,4);Grid.SetRow(txt1,0);Grid.SetColumn(txt1,1);// Add the second text cell to the Grid.txt2 =newTextBlock();txt2.Text="Open:";Grid.SetRow(txt2,1);Grid.SetColumn(txt2,0);// Add the TextBox control.tb1 =newTextBox();Grid.SetRow(tb1,1);Grid.SetColumn(tb1,1);Grid.SetColumnSpan(tb1,5);// Add the buttons.button1 =newButton();button2 =newButton();button3 =newButton();button1.Content="OK";button2.Content="Cancel";button3.Content="Browse ...";Grid.SetRow(button1,3);Grid.SetColumn(button1,2);button1.Margin=newThickness(10,0,10,15);button2.Margin=newThickness(10,0,10,15);button3.Margin=newThickness(10,0,10,15);Grid.SetRow(button2,3);Grid.SetColumn(button2,3);Grid.SetRow(button3,3);Grid.SetColumn(button3,4);grid1.Children.Add(img1);grid1.Children.Add(txt1);grid1.Children.Add(txt2);grid1.Children.Add(tb1);grid1.Children.Add(button1);grid1.Children.Add(button2);grid1.Children.Add(button3);