Exploring Windows Presentation Foundation: With Practical Applications in .NET 5 Taurius Litvinavicius - Quickly download the ebook in PDF format for unlimited reading
Exploring Windows Presentation Foundation: With Practical Applications in .NET 5 Taurius Litvinavicius - Quickly download the ebook in PDF format for unlimited reading
com
https://textbookfull.com/product/exploring-windows-
presentation-foundation-with-practical-applications-in-
net-5-taurius-litvinavicius/
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/exploring-blazor-creating-hosted-
server-side-and-client-side-applications-with-c-taurius-litvinavicius/
textbookfull.com
https://textbookfull.com/product/foundation-engineering-geotechnical-
principles-and-practical-applications-1st-edition-richard-handy/
textbookfull.com
Windows 11 Mastery: From Foundation to Mastery Kameron
Hussain
https://textbookfull.com/product/windows-11-mastery-from-foundation-
to-mastery-kameron-hussain/
textbookfull.com
https://textbookfull.com/product/pro-net-5-custom-libraries-
implementing-custom-net-data-types-1st-edition-roger-villela/
textbookfull.com
Exploring
Windows
Presentation
Foundation
With Practical Applications in .NET 5
—
Taurius Litvinavicius
Exploring Windows
Presentation
Foundation
With Practical
Applications in .NET 5
Taurius Litvinavicius
Exploring Windows Presentation Foundation: With Practical Applications
in .NET 5
Taurius Litvinavicius
Jonava, Lithuania
Chapter 2: Events�������������������������������������������������������������������������������33
Application Events����������������������������������������������������������������������������������������������33
Mouse Events������������������������������������������������������������������������������������������������������34
Keyboard Events�������������������������������������������������������������������������������������������������38
Window Events���������������������������������������������������������������������������������������������������39
Quick Example����������������������������������������������������������������������������������������������������41
Quick Exercise����������������������������������������������������������������������������������������������������44
iii
Table of Contents
Chapter 4: Files�����������������������������������������������������������������������������������85
Pick and Save�����������������������������������������������������������������������������������������������������85
Quick Example����������������������������������������������������������������������������������������������������89
Quick Exercise����������������������������������������������������������������������������������������������������95
Chapter 5: MVVM������������������������������������������������������������������������������101
Element to Element Binding������������������������������������������������������������������������������103
Introducing ViewModel�������������������������������������������������������������������������������������104
Implementing Models���������������������������������������������������������������������������������������109
Quick Example��������������������������������������������������������������������������������������������������116
Quick Exercise��������������������������������������������������������������������������������������������������136
Solution�������������������������������������������������������������������������������������������������������141
iv
Table of Contents
Chapter 6: Styles������������������������������������������������������������������������������151
Window Size and Other Sizes���������������������������������������������������������������������������151
Style������������������������������������������������������������������������������������������������������������������160
Quick Example��������������������������������������������������������������������������������������������������177
Quick Exercise��������������������������������������������������������������������������������������������������208
Solution�������������������������������������������������������������������������������������������������������210
Index�������������������������������������������������������������������������������������������������223
v
About the Author
Taurius Litvinavicius is a businessman and
technology expert based in Lithuania who
has worked with various organizations in
building and implementing projects in
software development, sales, and other fields
of business. He is responsible for technological
improvements, development of new features,
and general management. Taurius is also the
director at the Conficiens solutio consulting
agency where he supervises the development
and maintenance of various projects and
activities.
vii
About the Technical Reviewer
Carsten Thomsen is a back-end developer
primarily but working with smaller front-
end bits as well. He has authored and
reviewed a number of books, and created
numerous Microsoft Learning courses, all to
do with software development. He works as
a freelancer/contractor in various countries
in Europe, using Azure, Visual Studio, Azure
DevOps, and GitHub as some of his tools.
Being an exceptional troubleshooter, asking
the right questions, including the less logical
ones, in a most logical to least logical fashion, he also enjoys working with
architecture, research, analysis, development, testing, and bug fixing.
Carsten is a very good communicator with great mentoring and team lead
skills and great skills researching and presenting new material.
ix
Introduction
In this book, you will find lots of information about Windows Presentation
Foundation (WPF) which will help you get started with it. Alongside the
explanations of features, you will find use case examples and exercise
assignments for you to practice what you have learned.
The first chapter will provide you with a basic introduction to the
WPF. That will include handling button click event, window handling,
accessing text box inputs, and a few more things. In the next chapter, you
will see some generic events; some of them are related to the window,
some to the mouse, and some to other things. The third chapter will
cover various UI elements in WPF; it will also be useful to you for future
reference. It is important to read and understand the first chapter, but in
case you are in a hurry, you may skip the second and third chapters and
only use them as reference later.
The fourth chapter will show you how to handle files in the WPF
interface, and with that, it will provide some use cases to study. At this
point, the examples will incorporate quite a few items from the previous
chapters. Then in the fifth, you will see how the MVVM structure can be
implemented – the explanation will be a practical one; this should help
you understand MVVM quicker. The final chapter will cover the styling
aspects of WPF, but with that, it will also show examples that incorporate
most things that you can find in the previous chapters. Once again, if
you are rushing to get started with WPF and do not have too much time,
you may skip the MVVM chapter. But it would be a good idea to take a
look at it later, as it is useful to understand what MVVM is and how it is
implemented.
xi
CHAPTER 1
Getting Started
Windows Presentation Foundation (WPF) has many features and many
arrangements you can choose from, but there are a few crucial things that
you have to know before going anywhere else. For any user interaction
to be viable in WPF, you need to understand how to use the methods,
and with that, you also need to understand how to set properties on the
elements. In this chapter, we will begin with only a handful of them, and in
Chapter 3, you will see many more to choose from, and they function in a
very similar way. Before going further, you should also understand how to
establish and then display or close windows and how to display quick and
simple alert messages.
2
Chapter 1 Getting Started
Once you get into the MainWindow.xaml code, you will see a designer
view. Although you can set various properties in XAML, it is best to drag
and drop from the toolbox (Figure 1-2) and then move things around, expand
them, and do other things in the designer. Once you drag and drop something
onto the designer view, the XAML code for that element will be generated.
This is what our example (see Figure 1-3) will look like (after the
buttons are clicked).
<Window x:Class="WpfApp3.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"
3
Chapter 1 Getting Started
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="bt1" Content="first option"
HorizontalAlignment="Left" Margin="350,163,0,0"
VerticalAlignment="Top" Width="101"/>
<TextBlock x:Name="textblock1"
HorizontalAlignment="Left" Margin="350,198,0,0"
TextWrapping="Wrap" VerticalAlignment="Top"
Width="101"/>
This is the XAML code (Listing 1-1) for MainWindow.xaml; all the
elements go into the Grid element. The first element in the grid is a Button.
In it, we have a name property (you will see why we need it in the C# code);
after that, we have Content, which is the text displayed in the button.
After that, two very important properties are HorizontalAlignment and
VerticalAlignment, which are important because they determine the
alignment, and with that, you can use margins accordingly.
The next element is the TextBlock which is used to display text – the
crucial part here is the name, as that will be the reference point in C#. With
that, you can also see TextWrapping – if set, it will wrap the text onto a new
line; if not set, the default value will be used (NoWrap) and the text will be
displayed in a single line.
4
Chapter 1 Getting Started
Another Button, similar to the first one, but with no name specified.
Instead, we have the Click property which has a value of Button_Click –
this will correspond with the event method name in C#. So, you will
basically see two ways of declaring an event.
Finally, we have a label, which is an element very similar to a
TextBlock. But the label has fewer options in terms of customization.
In the C# code (code-behind) (see Listing 1-2), you can first see the
constructor method for the window class. In it, we declare the button click
event for the first button. In the code, the first event is for the first button and
the second for the second button. To set a display value for the TextBlock, you
need to set the Text property, but for the label, you set the Content property.
5
Chapter 1 Getting Started
W
indow and Page
In WPF there are two main view options – one is window, and the other is
page. Another option may be tabs, or you may simply change visibility of
grids and other containers, but that will be covered in the next chapters
(Chapters 3 and 6).
Both window and page will have XAML part and C# part (code-behind).
The main difference is how they are displayed and how the user navigates
between them.
6
Chapter 1 Getting Started
To create a new window, you need to right-click your project (or folder
in which you will place the file) and then go to add and choose Window
(WPF) (see Figure 1-4). Alternatively, you can find this option in “New Item”.
<Window x:Class="WpfApp.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:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="bt1" Content="window 1"
HorizontalAlignment="Left" Margin="246,118,0,0"
VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
The XAML part of a window (See Listing 1-3) will contain all the
markup for your elements and the window itself. By default, a window
will contain a grid and that is where you should place your elements – you
cannot do that in the window itself. In the default window, everything from
x:Class to mc:Ignorable should not be modified; otherwise, it may break.
You may change the title property (which appears at the top-left corner of
a window), and you can change the height and width properties. There are
many more things that you can do with a window, and some of them will
be covered in the last chapter of this book.
7
Chapter 1 Getting Started
public MainWindow()
{
InitializeComponent();
bt1.Click += Bt1_Click;
}
8
Chapter 1 Getting Started
In this example, we have added two new files (see Figure 1-5) – Pages.
Initially, you will see an empty window like this (Figure 1-6).
9
Chapter 1 Getting Started
Figure 1-7. Second state of the view (“To page 1” button clicked)
After the button is clicked, a page will be displayed (see Figure 1-7).
If the second button is clicked, the first page will be replaced with the
second one.
<Grid>
<Frame x:Name="fr" Margin="0,0,0,66" NavigationUI
Visibility="Hidden"/>
<Button Content="To page 1" Click="Button_Click"
HorizontalAlignment="Left" Margin="321,406,0,0"
VerticalAlignment="Top" Height="28" Width="79"/>
<Button Content="To page 2" Click="Button_Click_1"
HorizontalAlignment="Left" Margin="400,406,0,0"
VerticalAlignment="Top" Height="28" Width="79"/>
</Grid>
10
Chapter 1 Getting Started
In your window (see Listing 1-5) in which you want to display pages,
you will need a Frame element – that is where a page is displayed.
You need to set an appropriate size, establish a name, and then set
NavigationUIVisibility to Hidden. The last one is not important from
a functional perspective, but if not set, it would display a navigation bar,
similar to what a browser might have.
Navigating to a page is quite simple here (See Listing 1-6); you just
need to set the Content property of the Frame element.
public Page1()
{
InitializeComponent();
}
11
Chapter 1 Getting Started
Text Box
There are two basic interactive elements in WPF – the first one you already
know, which is a button, and the second one is the text box. In this book,
you will see more elements in the third chapter, but this will be your
starting point in understanding the input elements.
12
Chapter 1 Getting Started
In this case, we have six elements (see Figure 1-8 and Listing 1-8) in
the XAML (the grid is in the window). The first label simply has some
static text, the second element is a text box, and a text box needs a name
for referencing in C#. After that, we have another static label and then we
have another text box with a name. There is also another label with a name
(it will be used from C#) and finally a button with an event. Notice, in this
case, the event is declared in XAML; therefore, you will not see it declared
in the C# code. The idea here is to enter something into the input box (tb1)
and output the value in the label (output1) and text box (output2).
If you want to have multiple lines in your text box input, you need to set
the AcceptsReturn property to True. Also, by default, the text box does not
accept tab input; if you want that, you will need to set AcceptsTab="True".
13
Chapter 1 Getting Started
This is the event on button click in the C# part (see Listing 1-9). Now,
the output1 is a label and a label has the property Content, which is what
you assign to display something. On the other hand, text box has the Text
property, which will hold the input and can also be assigned to display
something. The most important thing in any input element is to know
which property holds the value – in this case, it is quite easy to find, but
later, you will see some elements that are a bit more tricky.
Message Box
Message box is a great way to display basic notifications and alerts
to the user; it is simple to use and can be a great safeguard on some
occasions. Now, we will look at how it is implemented, but before that,
you have to know about the possible arrangements of this feature. There
are four default options in a message in terms of the buttons it may
contain. Each button, when clicked, will close the box and return a result
(MessageBoxResult). Later in this book, you will learn how to make your
own custom message box (refer to Chapter 6).
14
Chapter 1 Getting Started
This option (see Figure 1-10) is best for when the user has to leave (or
clear) something. In those arrangements, this would be the last chance for
them to avoid data loss.
15
Chapter 1 Getting Started
The final option (see Figure 1-12) is the most elaborate one, and it
can be used in various scenarios. It is unlikely to be the most common
type, but it is the only one that has three buttons, hence three different
possible outcomes. We will look at a code example of this message box
implemented.
<Grid>
<Button x:Name="mbtestbt" Content="test"
HorizontalAlignment="Left" Margin="360,179,0,0"
VerticalAlignment="Top" Width="75"/>
</Grid>
In the XAML part (See Listing 1-10), we only have one button defined.
We also have a name for the button, which we will use to establish an
event. Do remember that an event like this could be defined on the button
in the XAML.
16
Chapter 1 Getting Started
public MainWindow()
{
InitializeComponent();
mbtestbt.Click += Mbtestbt_Click;
}
17
Chapter 1 Getting Started
Q
uick Example
To wrap things up with the basics, we have a very basic example. This will
show you how some of these elements and feature of WPF can interact
together.
As you can see (see Figure 1-13), we have a simple registration form.
We will not look at the full functionality of such an arrangement, but you
18
Chapter 1 Getting Started
will see how these elements can work to create something useful. The
Generate button will generate a five-digit password, and the Copy button
will copy it to the clipboard. On the Submit button, we do send the data
anywhere, but we do check for the password to be longer than three
characters (i.e., at least four characters).
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="Registration window" Height="450" Width="415">
<Grid HorizontalAlignment="Center" Width="415">
<Label Content="Full name" HorizontalAlignment="Center"
Margin="0,62,0,0" VerticalAlignment="Top" Width="147"/>
<TextBox x:Name="tb_fullname"
HorizontalAlignment="Left" Margin="134,93,0,0"
TextWrapping="Wrap" VerticalAlignment="Top"
Width="120"/>
<Label Content="Email" HorizontalAlignment="Center"
Margin="0,136,0,0" VerticalAlignment="Top"
Width="147"/>
<TextBox x:Name="tb_email" HorizontalAlignment="Left"
Margin="134,167,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="120"/>
19
Chapter 1 Getting Started
</Grid>
</Window>
The first thing you may notice in the XAML code (See Listing 1-12)
is the window title, and then you can see that the width is set to 415. The
labels in this case are simply for displaying static values. After that, notice
how the text boxes and buttons are named. The text boxes start with tb,
and the buttons start with bt; this helps when you have to type in the
names in the C# code. Also, when you have a name for the button and
create an event through XAML, a very appropriate name for the event will
be generated.
20
Exploring the Variety of Random
Documents with Different Content
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com