First of all, Sorry for this lame question. However, I am learning WPF on my own. I am learning about Panel
class now. To learn this I am going through this MSDN web-page. The way MSDN has described to create a Panel
using code appeals me a lot. for instance
// Create the application's main window
mainWindow = new Window ();
mainWindow.Title = "Canvas Sample";
// Create the Canvas
myParentCanvas = new Canvas();
myParentCanvas.Width = 400;
myParentCanvas.Height = 400;
// Define child Canvas elements
myCanvas1 = new Canvas();
myCanvas1.Background = Brushes.Red;
Canvas.SetLeft(myCanvas1, 0);
.
.
.
myParentCanvas.Children.Add(myCanvas3);
// Add the parent Canvas as the Content of the Window Object
mainWindow.Content = myParentCanvas;
mainWindow.Show ();
I want to create Panel
(layout) in the way shown on the page, but I am confused where should I write (copy-paste) this code. I am well conversant creating Layout
using XAML
At filename.xaml.cs
we have got following lines of code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
I tried to put this (MSDN's) lines of code inside MainWindow
method, but it did not help me much. According to my little knowledge, we should have a method like mainWindowContent()
and this should be call along InitializeComponent()
or similar (I might be wrong with this).
Kindly guide me with the correct way to achieve this.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Title = "Canvas Sample";
// Create the Canvas
var myParentCanvas = new Canvas();
myParentCanvas.Width = 400;
myParentCanvas.Height = 400;
// Define child Canvas elements
var myCanvas1 = new Canvas();
myCanvas1.Background = Brushes.Red;
myCanvas1.Height = 100;
myCanvas1.Width = 100;
Canvas.SetTop(myCanvas1, 0);
Canvas.SetLeft(myCanvas1, 0);
var myCanvas2 = new Canvas();
myCanvas2.Background = Brushes.Green;
myCanvas2.Height = 100;
myCanvas2.Width = 100;
Canvas.SetTop(myCanvas2, 100);
Canvas.SetLeft(myCanvas2, 100);
var myCanvas3 = new Canvas();
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 collection
myParentCanvas.Children.Add(myCanvas1);
myParentCanvas.Children.Add(myCanvas2);
myParentCanvas.Children.Add(myCanvas3);
// Add the parent Canvas as the Content of the Window Object
this.Content = myParentCanvas;
}
}
is equivalent to
<Window Title="Canvas Sample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas Height="400" Width="400">
<Canvas Height="100" Width="100" Top="0" Left="0" Background="Red"/>
<Canvas Height="100" Width="100" Top="100" Left="100" Background="Green"/>
<Canvas Height="100" Width="100" Top="50" Left="50" Background="Blue"/>
</Canvas>
</Window>
If done within the MainWindow
code-behind itself.
And then on start up you can just call the class
// Create the application's main window
var mainWindow = new MainWindow ();
mainWindow.Show ();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments