Graphics Reference
In-Depth Information
and
</Button>
tags, we have the button's content, which is just a simple piece
of text.
The
Slider
in lines 25-30 is similar: We've set several options, specifying
the slider's width, its initial value (0), its maximum value (20, when the slider
thumb is all the way to the right), the fact that it should be horizontal, where
to place tick marks, and how many to place. The most interesting feature is
the
ValueChanged=slider1change
, which means that when the user changes the
slider's value, WPF should call the
slider1change
method of the
Window1
class.
We've now examined almost all the XAML code. You could probably, with
some confidence, edit this file to add a few more buttons and a few more sliders,
or even figure out how to change the colors of the buttons, or break the
StackPanel
into a pair of
StackPanels
, one for buttons and one for sliders, side by side. (Hint:
You could include both in a new
DockPanel
with its orientation being horizontal.)
Inline Exercise 4.2:
Add a new button or slider to the
StackPanel
, but don't
include the
Click=
or
ValueChanged=
, respectively. Run your program to
ensure that the new item actually appears where you expect it.
Inline Exercise 4.3:
Rearrange the Controls
StackPanel
, as suggested above,
into one panel full of buttons and another panel containing only sliders. Verify
that your modification works.
If you were to add a new button, set
Click=b3Click
, and then try to compile
the application, it would fail because you'd need to write the
b3click
method of
the
Window1
class; we'll discuss this in more detail in a moment.
Inline Exercise 4.4:
Add a button with
Click=b3Click
, and verify that the
application no longer works. Try to parse the error message and make sense of
it. Then remove the new button.
The last item in the XAML code is a
GraphPaper
. The syntax here is a little
peculiar. Listing 4.4 shows the XAML code, much reduced.
Listing 4.4: The part of
Window1.xaml
that creates a
GraphPaper
.
1
2
3
4
5
6
7
8
9
10
11
<Window
x:Class=
"Testbed2D.Window1"
xmlns= ...
"
xmlns:h="clr-namespace:Testbed2D"
...>
...
<
h:GraphPaper
x:Name="
Paper
">
</h:GraphPaper>
</DockPanel>
</Window>
The highlighted
xmlns
line indicates that the XML namespace called
h
refers
to the Common Language Runtime (
clr
) namespace defined by
Testbed2D
.This
means that
GraphPaper
is not a standard WPF class, but rather a class defined by
this project, a class called
GraphPaper
instead of WPF's
Canvas
class. In fact, a
GraphPaper
is a lot like a
Canvas
(it is, in fact,
derived
from
Canvas
), except that it