- C# WPF Introduction
- C# WPF First Program
- C# WPF without XAML – Window Class
- C# WPF First Window Introduction
- C# WPF Create a Simple Binding Object in Code Behind
- C# WPF Data Binding
- C# WPF Data Binding and Triggers
- C# WPF Data Binding and Binding Direction
- C# WPF Data Binding and Triggers MouseOvers
- C# WPF Value Converters
- C# WPF ComboBox Control
- C# WPF Bindings and ItemsControls
- C# WPF Styles
- C# WPF RGB Colour Viewer
- C# WPF XAML Background Colour
- C# WPF Events
- C# WPF Dynamic Binding to External Objects
- C# WPF Multiple Bindings on an Object
Just to get started, the screen shot of our first program is shown here. It is our version of a Hello World program. As simple as possible. Using Visual Studio 2015, we created a new WPF program using C#. Then a button was dragged on to the Designer window. The dimensions of the main window ware changed. The Title of the main window was changed. The name of the button was changed from button to mybutton. A click event was added to the button by double-clicking the button in the designer. Message box code was added to the MainWindow.xaml.cs file.
This post is part of a series of posts that fall under the topic XAML. In future posts we will talk more about what XAML is.
The code for this project is shown below.
<Window x:Class="WPF01.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPF01" mc:Ignorable="d" Title="My First WPF Program" Height="200" Width="300"> <Grid Margin="0,0,0,0"> <Button x:Name="mybutton" Content="Button" HorizontalAlignment="Left" Margin="40,20,0,0" VerticalAlignment="Top" Width="75" Click="mybutton_Click"/> </Grid> </Window>
Notice in the above code that I added a Title called “My First WPF Program”. It’s just text so you could put what you want. I also changed the Height and Width to make it smaller than the default. So it’s not as simple as possible, but you need to know what program you are looking at and the title is a good place to start.
Here below is the code behind. How do you get to see that code? Click on the tab at the top called MainWindow.xaml.cs This is actually a text file in your project.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WPF01 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void mybutton_Click(object sender, RoutedEventArgs e) { // double click the button on the Designer to get this MessageBox.Show("You clicked me!"); } } }
Notice that using System.Windows; is the only using statement in the upper section that is bold. It’s the only one you really need to run this particular program.