Académique Documents
Professionnel Documents
Culture Documents
WPF Tutorial
Page 1 of 359
wpf-tutorial.com
Page 2 of 359
wpf-tutorial.com
wpf-tutorial.com
Page 4 of 359
wpf-tutorial.com
Page 5 of 359
wpf-tutorial.com
<Window x:Class="WpfApplication1.MainWindow"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
This is the base XAML that Visual Studio creates for our window, all parts of it explained in the chapters on
XAML and "The Window". You can actually run the application now (select Debug -> Start debugging or
press F5) to see the empty window that our application currently consists of, but now it's time to get our
message on the screen. We'll do it by adding a TextBlock control to the Grid panel, with our
aforementioned message as the content:
<Window x:Class="WpfApplication1.MainWindow"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
Page 6 of 359
wpf-tutorial.com
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment
="Center" FontSize="72">
Hello, WPF!
</TextBlock>
</Grid>
</Window>
Try running the application now (select Debug -> Start debugging or press F5) and see the beautiful result
of your hard work - your first WPF application:
You will notice that we used three different attributes on the TextBlock to get a custom alignment (in the
middle of the window), as well the FontSize property to get bigger text. All of these concepts will be treated
in later articles.
Congratulations on making it this far. Now go read the rest of the tutorial and soon you will master WPF!
Page 7 of 359
wpf-tutorial.com
1.3. XAML
1.3.1. What is XAML?
XAML, which stands for eXtensible Application Markup Language, is Microsoft's variant of XML for
describing a GUI. In previous GUI frameworks, like WinForms, a GUI was created in the same language
that you would use for interacting with the GUI, e.g. C# or VB.NET and usually maintained by the designer
(e.g. Visual Studio), but with XAML, Microsoft is going another way. Much like with HTML, you are able to
easily write and edit your GUI.
This is not really a XAML tutorial, but I will briefly tell you about how you use it, because it's such an
essential part of WPF. Whether you're creating a Window or a Page, it will consist of a XAML document
and a CodeBehind file, which together creates the Window/Page. The XAML file describes the interface
with all its elements, while the CodeBehind handles all the events and has access to manipulate with the
XAML controls.
In the next chapters, we will have a look at how XAML works and how you use it to create your interface.
Page 8 of 359
wpf-tutorial.com
<Button>
XAML tags has to be ended, either by writing the end tag or by putting a forward slash at the end of the
start tag:
<Button></Button>
Or
<Button />
A lot of controls allow you to put content between the start and end tags, which is then the content of the
control. For instance, the Button control allows you to specify the text shown on it between the start and
end tags:
<Button>A button</Button>
HTML is not case-sensitive, but XAML is, because the control name has to correspond to a type in the
.NET framework. The same goes for attribute names, which corresponds to the properties of the control.
Here's a button where we define a couple of properties by adding attributes to the tag:
<Button>
<Button.FontWeight>Bold</Button.FontWeight>
<Button.Content>A button</Button.Content>
</Button>
The result is exactly the same as above, so in this case, it's all about syntax and nothing else. However, a
lot of controls allow content other than text, for instance other controls. Here's an example where we have
text in different colors on the same button by using several TextBlock controls inside of the Button:
<Button>
Page 9 of 359
wpf-tutorial.com
<Button.FontWeight>Bold</Button.FontWeight>
<Button.Content>
<WrapPanel>
<TextBlock Foreground="Blue">Multi</TextBlock>
<TextBlock Foreground="Red">Color</TextBlock>
<TextBlock>Button</TextBlock>
</WrapPanel>
</Button.Content>
</Button>
The Content property only allows for a single child element, so we use a WrapPanel to contain the
differently colored blocks of text. Panels, like the WrapPanel, plays an important role in WPF and we will
discuss them in much more details later on - for now, just consider them as containers for other controls.
The exact same result can be accomplished with the following markup, which is simply another way of
writing the same:
<Button FontWeight="Bold">
<WrapPanel>
<TextBlock Foreground="Blue">Multi</TextBlock>
<TextBlock Foreground="Red">Color</TextBlock>
<TextBlock>Button</TextBlock>
</WrapPanel>
</Button>
1.3.2.1. Code vs. XAML
Hopefully the above examples show you that XAML is pretty easy to write, but with a lot of different ways of
doing it, and if you think that the above example is a lot of markup to get a button with text in different
colors, then try comparing it to doing the exact same thing in C#:
wpf-tutorial.com
pnl.Children.Add(txt);
txt = new TextBlock();
txt.Text = "Button";
pnl.Children.Add(txt);
btn.Content = pnl;
pnlMain.Children.Add(btn);
Of course the above example could be written less explicitly and using more syntactical sugar, but I think
the point still stands: XAML is pretty short and concise for describing interfaces.
Page 11 of 359
wpf-tutorial.com
<Window x:Class="WpfTutorialSamples.XAML.EventsSample"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EventsSample" Height="300" Width="300">
<Grid Name="pnlMainGrid" MouseUp="pnlMainGrid_MouseUp" Background
="LightBlue">
</Grid>
</Window>
Notice how we have subscribed to the MouseUp event of the Grid by writing a method name. This method
needs to be defined in code-behind, using the correct event signature. In this case it should look like this:
wpf-tutorial.com
When you select <New Event Handler> Visual Studio will generate an appropriate event handler in
your Code-behind file. It will be named <control name>_<event name>, in our case
pnlMainGrid_MouseDown. Right-click in the event name and select Navigate to Event Handler and VS
will take you right to it.
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfTutorialSamples.XAML
{
public partial class EventsSample : Window
{
public EventsSample()
{
InitializeComponent();
pnlMainGrid.MouseUp += new
MouseButtonEventHandler(pnlMainGrid_MouseUp);
}
Page 13 of 359
wpf-tutorial.com
Simply press the [Tab] key twice to have Visual Studio generate the correct event handler for you, right
below the current method, ready for imeplentation. When you subscribe to the events like this, you don't
need to do it in XAML.
Page 14 of 359
wpf-tutorial.com
Page 15 of 359
wpf-tutorial.com
This was a free sample - please buy the full version to read the rest of this
tutorial!
More information at http://www.wpf-tutorial.net/