A WPF Code-Behind Basis Program

2026-11-03

Summary

For getting a WPF C# only program it's always nice to have a template for which to get started. This example gives a basis for that. If you need a brush up on how to compile a simple remote WPF program, see here: A Basic C# only WPF program.

	
		/*
Author:			Cuauhtlatoa - Property of Patlani Technologies
Date:			2026-11-03
Purpose:		Serves as a basis for a basis of code-behind only WPF program. Gives a set of basic controls and gives allows user to use them. It also contains basic styling.
*/

// Program directives
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Demo
{
	// Main window
	public partial class MainWindow : Window
	{
		const int hgt = 400;
		const int wdt = 400;

		// Set default constructor
		public MainWindow()
		{
			this.Title = "WPF Basis Program";
			this.Width = wdt;
			this.Height = hgt;
			
			// Set up the grid
			Grid myGrid = new Grid();
			myGrid.Width = wdt;
			myGrid.Height = hgt;
			myGrid.HorizontalAlignment = HorizontalAlignment.Left;
			myGrid.VerticalAlignment = VerticalAlignment.Top;
			myGrid.ShowGridLines = true;

			// Set up columns
			ColumnDefinition col0 = new ColumnDefinition();
			ColumnDefinition col1 = new ColumnDefinition();
			ColumnDefinition col2 = new ColumnDefinition();
			ColumnDefinition col3 = new ColumnDefinition();
			ColumnDefinition col4 = new ColumnDefinition();
			myGrid.ColumnDefinitions.Add(col0);
			myGrid.ColumnDefinitions.Add(col1);
			myGrid.ColumnDefinitions.Add(col2);
			myGrid.ColumnDefinitions.Add(col3);
			//col0.Width = new GridLength(10);
			//col1.Width = new GridLength(70);
			//col2.Width = new GridLength(20);
			//col3.Width = new GridLength(70);
			//col4.Width = new GridLength(1,GridUnitType.Star);

			// Set Grid rows
            RowDefinition row0 = new RowDefinition();
            RowDefinition row1 = new RowDefinition();
            RowDefinition row2 = new RowDefinition();
			RowDefinition row3 = new RowDefinition();
            myGrid.RowDefinitions.Add(row0);
            myGrid.RowDefinitions.Add(row1);
			myGrid.RowDefinitions.Add(row2);
			myGrid.RowDefinitions.Add(row3);
            row0.Height = new GridLength(70);
            row1.Height = new GridLength(1,GridUnitType.Star);
            row2.Height = new GridLength(30);
			row3.Height = new GridLength(100);
			
			// Create button windows
			// Create a button that displays an error window
			Button showErr = new Button();
			showErr.Content = "Show Error";
			showErr.Click += SimErr;
			Grid.SetRow(showErr,2);
			Grid.SetColumn(showErr,1);
			myGrid.Children.Add(showErr);

			// Create a button that closes the window
			Button closeBTN = new Button();
			closeBTN.Content = "Close";
			closeBTN.Click += CloseProgram;
			Grid.SetRow(closeBTN,2);
			Grid.SetColumn(closeBTN,3);
			myGrid.Children.Add(closeBTN);
			
			// TextBox that allows user to freely type
			TextBox tb = new TextBox();
			
			// Color schemes for the TextBox
			// Font color
			Color col = new Color();
            col.A = 255;
            col.R = 255;
            col.G = 255;
            col.B = 255;
            SolidColorBrush tbFontBrush = new SolidColorBrush(col);
			tb.Foreground = tbFontBrush;
			
			// Background color
			col = new Color();
            col.A = 255;
            col.R = 0;
            col.G = 0;
            col.B = 0;
			SolidColorBrush tbBkBrush = new SolidColorBrush(col);
            tb.Background = tbBkBrush;
			
			// Finish setting up the TextBox
			Grid.SetRow(tb,1);
			Grid.SetColumn(tb,0);
			Grid.SetColumnSpan(tb,5);
			myGrid.Children.Add(tb);
			tb.TextWrapping = TextWrapping.Wrap;
			tb.Text = "Type here!";
			
			// Label the TextBox
			Label lbl = new Label();
			lbl.Content = "Type Text Below";
			lbl.VerticalAlignment = VerticalAlignment.Bottom;
			Grid.SetRow(lbl,0);
			Grid.SetColumn(lbl,0);
			Grid.SetColumnSpan(lbl,5);
			myGrid.Children.Add(lbl);
            

			// Display all grid content
			this.Content = myGrid;

		}


		// Simulate error window
		public void SimErr(object sender, EventArgs e)
		{
			MessageBox.Show("Be sure to use message boxes to troubleshoot errors!");
		}

		// Button to close window
		public void CloseProgram(object sender, EventArgs e)
		{
			this.Close();
		}




	}




		// Main program structure
		public class MainProgram
		{
			[STAThread]
			static void Main()
			{
				Application mp = new Application();
				MainWindow mw = new MainWindow();
				mw.Show();
				mp.Run();
			}
		}




}