FANDOM


From : http://www.tbiro.com/Hello-WPF-Without-XAML.htm

  1. Create a WPF application and delete the files.
  2. Add file: MyWindow.cs:
using System;
using System.Windows;
using System.Windows.Controls;

namespace HelloWorldManual
{
    public class MyWindow : Window
    {
        private Label label1;

        public MyWindow()
        {
            this.Width = 300;
            this.Height = 300;

            Grid grid = new Grid();
            this.Content = grid;

            Button button1 = new Button();
            button1.Content = "Say Hello!";
            button1.Height = 23;
            button1.Margin = new Thickness(96, 50, 107, 0);
            button1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            button1.Click += new RoutedEventHandler(button1_Click);
            grid.Children.Add(button1);

            label1 = new Label();
            label1.Margin = new Thickness(84,115,74,119);
            grid.Children.Add(label1);
        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            label1.Content = "Hello WPF!";
        }

        [STAThread]
        public static void Main()
        {
            Application app = new Application();

            app.Run(new MyWindow());
        }
    }
}


Junk ExampleEdit

Instead of the above source, use the following for MyWindow.cs:

None of this make sense below, but it compiles and it will create a bunch of Windows with various contents.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;

namespace HelloWorldManual
{
    public partial class CodeOnlyWindow : Window
    {
        public CodeOnlyWindow()
        {
            this.Title = "Main Window in Code Only";
            this.Width = 300;
            this.Height = 300;
        }
    }

    public class GridExample
    {
        public static void CreateAndShow()
        {
            // Create the application's main window
            var mainWindow = new Window();
            mainWindow.Title = "Grid Sample";

            // Create the Grid
            Grid myGrid = new Grid();
            myGrid.Width = 250;
            myGrid.Height = 100;
            myGrid.HorizontalAlignment = HorizontalAlignment.Left;
            myGrid.VerticalAlignment = VerticalAlignment.Top;
            myGrid.ShowGridLines = true;

            // Define the Columns
            ColumnDefinition colDef1 = new ColumnDefinition();
            ColumnDefinition colDef2 = new ColumnDefinition();
            ColumnDefinition colDef3 = new ColumnDefinition();
            myGrid.ColumnDefinitions.Add(colDef1);
            myGrid.ColumnDefinitions.Add(colDef2);
            myGrid.ColumnDefinitions.Add(colDef3);

            // Define the Rows
            RowDefinition rowDef1 = new RowDefinition();
            RowDefinition rowDef2 = new RowDefinition();
            RowDefinition rowDef3 = new RowDefinition();
            RowDefinition rowDef4 = new RowDefinition();
            myGrid.RowDefinitions.Add(rowDef1);
            myGrid.RowDefinitions.Add(rowDef2);
            myGrid.RowDefinitions.Add(rowDef3);
            myGrid.RowDefinitions.Add(rowDef4);

            // Add the first text cell to the Grid
            TextBlock txt1 = new TextBlock();
            txt1.Text = "2005 Products Shipped";
            txt1.FontSize = 20;
            txt1.FontWeight = FontWeights.Bold;
            Grid.SetColumnSpan(txt1, 3);
            Grid.SetRow(txt1, 0);

            // Add the second text cell to the Grid
            TextBlock txt2 = new TextBlock();
            txt2.Text = "Quarter 1";
            txt2.FontSize = 12;
            txt2.FontWeight = FontWeights.Bold;
            Grid.SetRow(txt2, 1);
            Grid.SetColumn(txt2, 0);

            // Add the third text cell to the Grid
            TextBlock txt3 = new TextBlock();
            txt3.Text = "Quarter 2";
            txt3.FontSize = 12;
            txt3.FontWeight = FontWeights.Bold;
            Grid.SetRow(txt3, 1);
            Grid.SetColumn(txt3, 1);

            // Add the fourth text cell to the Grid
            TextBlock txt4 = new TextBlock();
            txt4.Text = "Quarter 3";
            txt4.FontSize = 12;
            txt4.FontWeight = FontWeights.Bold;
            Grid.SetRow(txt4, 1);
            Grid.SetColumn(txt4, 2);

            // Add the sixth text cell to the Grid
            TextBlock txt5 = new TextBlock();
            Double db1 = new Double();
            db1 = 50000;
            txt5.Text = db1.ToString();
            Grid.SetRow(txt5, 2);
            Grid.SetColumn(txt5, 0);

            // Add the seventh text cell to the Grid
            TextBlock txt6 = new TextBlock();
            Double db2 = new Double();
            db2 = 100000;
            txt6.Text = db2.ToString();
            Grid.SetRow(txt6, 2);
            Grid.SetColumn(txt6, 1);

            // Add the final text cell to the Grid
            TextBlock txt7 = new TextBlock();
            Double db3 = new Double();
            db3 = 150000;
            txt7.Text = db3.ToString();
            Grid.SetRow(txt7, 2);
            Grid.SetColumn(txt7, 2);

            // Total all Data and Span Three Columns
            TextBlock txt8 = new TextBlock();
            txt8.FontSize = 16;
            txt8.FontWeight = FontWeights.Bold;
            txt8.Text = "Total Units: " + (db1 + db2 + db3).ToString();
            Grid.SetRow(txt8, 3);
            Grid.SetColumnSpan(txt8, 3);

            // Add the TextBlock elements to the Grid Children collection
            myGrid.Children.Add(txt1);
            myGrid.Children.Add(txt2);
            myGrid.Children.Add(txt3);
            myGrid.Children.Add(txt4);
            myGrid.Children.Add(txt5);
            myGrid.Children.Add(txt6);
            myGrid.Children.Add(txt7);
            myGrid.Children.Add(txt8);

            // Add the Grid as the Content of the Parent Window Object
            mainWindow.Content = myGrid;
            mainWindow.Show();
        }
    }

    public class MyWindow : Window
    {
        private Label label1;

        public MyWindow()
        {
            this.Width = 800;
            this.Height = 600;

            Grid grid = new Grid();
            this.Content = grid;

            Button button1 = new Button();
            button1.Content = "Say Hello!";
            button1.Height = 23;
            button1.Margin = new Thickness(96, 50, 107, 0);
            button1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            button1.Click += new RoutedEventHandler(button1_Click);
            grid.Children.Add(button1);

            label1 = new Label();
            label1.Margin = new Thickness(84,115,74,119);
            grid.Children.Add(label1);


            // Create a Button with a string as its content.
            Button stringContent = new Button();
            stringContent.Content = "This is string content of a Button";
            stringContent.Height = 40;
            stringContent.Width = 200;
            stringContent.Margin = new Thickness(10, 100, 107, 0);
            stringContent.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            stringContent.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            grid.Children.Add(stringContent);

            // Create a Button with a DateTime object as its content.
            Button objectContent = new Button();
            objectContent.Height = 40;
            objectContent.Width = 200;
            objectContent.Margin = new Thickness(10, 150, 107, 0);
            objectContent.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            objectContent.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            DateTime dateTime1 = new DateTime(2004, 3, 4, 13, 6, 55);
            grid.Children.Add(objectContent);

            objectContent.Content = dateTime1;

            // Create a Button with a single UIElement as its content.
            Button uiElementContent = new Button();
            uiElementContent.Margin = new Thickness(10, 200, 107, 0);

            Rectangle rect1 = new Rectangle();
            rect1.Width = 40;
            rect1.Height = 40;
            rect1.Fill = Brushes.Blue;
            uiElementContent.Content = rect1;

            uiElementContent.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            uiElementContent.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            grid.Children.Add(uiElementContent);


            // Create a Button with a panel that contains multiple objects 
            // as its content.
            Button panelContent = new Button();
            StackPanel stackPanel1 = new StackPanel();
            Ellipse ellipse1 = new Ellipse();
            TextBlock textBlock1 = new TextBlock();

            ellipse1.Width = 40;
            ellipse1.Height = 40;
            ellipse1.Fill = Brushes.Blue;

            textBlock1.TextAlignment = TextAlignment.Center;
            textBlock1.Text = "Button";

            stackPanel1.Children.Add(ellipse1);
            stackPanel1.Children.Add(textBlock1);

            panelContent.Content = stackPanel1;

            panelContent.Margin = new Thickness(10, 300, 0, 0);
            panelContent.Height = 100;
            panelContent.Width = 200;
            panelContent.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            panelContent.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            grid.Children.Add(panelContent);
        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            label1.Content = "Hello WPF!";
        }

        [STAThread]
        public static void Main()
        {
            Application app = new Application();

            GridExample.CreateAndShow();

            var w = new CodeOnlyWindow();
            w.Show();

            var w2 = new Window();
            w2.Show();

            app.Run(new MyWindow());
        }
    }

}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.