How to achieve the following in WPF

Nikhil G

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.

Nkosi
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.

edited at
0

Comments

0 comments
Login to comment

Related

How to achieve the following layout?

How to achieve the following in Realm for android

How to achieve the following structure in javafx?

How to achieve following design in html

How I achieve the following layout

How to run a single query to achieve the following result?

How to achieve the following sql query in influx?

How to achieve following result with Group By in MySql?

How to customise UIDatePicker to achieve the following output?

How to achieve the following code using List Comprehensions?

How to achieve following output in Mongo Aggregation?

How to create following styles in wpf?

How to achieve (*) sign pattern in WPF textbox

How to achieve Oculus UI in .Net WPF

How to achieve the following layout with dynamic width in Constraint Layout?

How can I achieve the following pattern using the DateTimeFormatter?

How to use substitute command in unix(vi or sed) to achieve the following results

How to achieve the following? C++ "advanced" map/list/array?

How to fix the following partition layout to achieve what I want

How to achieve the following using functions/stored procedure in SQL Server database

How can I achieve the following in oracle without creating a stored procedure?

How do i achieve the following dynamic route in next js?

How can I Achieve the following output with dynamic input values?

WPF-mvvm. How can I achieve such view (design, gui)?

How to achieve dynamic binding in WPF/MVVC C#

CSS: Is it possible to achieve the following layout?

Using SQL FOR XML PATH, how can I achieve the following formatted XML output?

How to achieve smooth movement on an object locked on the Y-axis following mouse position?

How does Semantic UI achieve the 'following' behaviour the accordion menu on its site?

TOP Ranking

HotTag

Archive