2010 - Pro WPF in C# 2010 (Apress).pdf

(14452 KB) Pobierz
568141872 UNPDF
Pro WPF in C# 2010:
Windows Presentation Foundation in
.NET 4.0
■ ■ ■
Matthew MacDonald
568141872.001.png
CONTENTS
Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4.0
Copyright © 2010 by Matthew MacDonald
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information
storage or retrieval system, without the prior written permission of the copyright owner and the
publisher.
ISBN-13 (pbk): 978-1-4302-7205-2
ISBN-13 (electronic): 978-1-4302-7204-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Publisher and President: Paul Manning
Lead Editor: Ewan Buckingham
Technical Reviewer: Fabio Claudio Ferracchiati
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic
Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Anne Collett
Copy Editor: Marilyn Smith and Kim Wimpsett
Compositor: ContentWorks, Inc. and Bob Cooper
Indexer: BIM Indexing & Proofreading Services
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th
Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-
sbm.com , or visit www.springeronline.com .
For information on translations, please e-mail rights@apress.com , or visit www.apress.com .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference
our Special Bulk Sales—eBook Licensing web page at www.apress.com/info/bulksales .
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to be
caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com . You will need to answer
questions pertaining to this book in order to successfully download the code.
ii
568141872.002.png
CONTENTS
Contents
Chapter 1: Introducing WPF ............................................................................................................... 1
The Evolution of Windows Graphics................................................................................................ 1
DirectX: The New Graphics Engine ............................................................................................. 2
Hardware Acceleration and WPF ................................................................................................ 3
WPF: A Higher-Level API ................................................................................................................ 4
Windows Forms Lives On ........................................................................................................... 6
DirectX Also Lives On ................................................................................................................. 6
Silverlight ................................................................................................................................... 6
Resolution Independence ............................................................................................................... 7
WPF Units .................................................................................................................................. 8
System DPI ................................................................................................................................ 9
Bitmap and Vector Graphics ..................................................................................................... 12
The Architecture of WPF............................................................................................................... 12
The Class Hierarchy ................................................................................................................. 14
WPF 4 .......................................................................................................................................... 17
New Features........................................................................................................................... 17
The WPF Toolkit ....................................................................................................................... 18
Visual Studio 2010 ................................................................................................................... 18
Chapter 2: XAML .............................................................................................................................. 23
Understanding XAML.................................................................................................................... 24
The Last Word.............................................................................................................................. 21
Graphical User Interfaces Before WPF ...................................................................................... 24
The Variants of XAML ............................................................................................................... 25
XAML Compilation.................................................................................................................... 26
XAML Basics ................................................................................................................................ 27
XAML Namespaces .................................................................................................................. 28
iv
568141872.003.png
CONTENTS
The Code-Behind Class ............................................................................................................ 30
Properties and Events in XAML..................................................................................................... 32
Simple Properties and Type Converters .................................................................................... 34
Complex Properties .................................................................................................................. 35
Markup Extensions................................................................................................................... 37
Attached Properties.................................................................................................................. 38
Nesting Elements ..................................................................................................................... 39
Special Characters and Whitespace ......................................................................................... 42
Events ...................................................................................................................................... 44
The Full Eight Ball Example ...................................................................................................... 45
Using Types from Other Namespaces........................................................................................... 46
Loading and Compiling XAML....................................................................................................... 48
Code-Only ................................................................................................................................ 48
Code and Uncompiled XAML .................................................................................................... 51
Code and Compiled XAML ........................................................................................................ 53
XAML Only ............................................................................................................................... 55
XAML 2009 .................................................................................................................................. 56
Automatic Event Hookup .......................................................................................................... 56
References............................................................................................................................... 58
Built-in Types........................................................................................................................... 58
Advanced Object Creation ........................................................................................................ 59
Chapter 3: Layout............................................................................................................................. 61
Understanding Layout in WPF....................................................................................................... 61
The WPF Layout Philosophy ..................................................................................................... 62
The Layout Process.................................................................................................................. 63
The Layout Containers ............................................................................................................. 63
Simple Layout with the StackPanel............................................................................................... 65
Layout Properties ..................................................................................................................... 67
Alignment ................................................................................................................................ 68
Margin ..................................................................................................................................... 69
Minimum, Maximum, and Explicit Sizes ................................................................................... 71
v
The Last Word.............................................................................................................................. 60
CONTENTS
The Border............................................................................................................................... 74
The WrapPanel and DockPanel..................................................................................................... 75
The WrapPanel......................................................................................................................... 76
The DockPanel......................................................................................................................... 77
Nesting Layout Containers ....................................................................................................... 79
The Grid ....................................................................................................................................... 80
Fine-Tuning Rows and Columns............................................................................................... 83
Layout Rounding ...................................................................................................................... 85
Spanning Rows and Columns................................................................................................... 86
Split Windows .......................................................................................................................... 87
Shared Size Groups.................................................................................................................. 91
The UniformGrid....................................................................................................................... 93
Coordinate-Based Layout with the Canvas ................................................................................... 94
Z-Order .................................................................................................................................... 95
The InkCanvas ......................................................................................................................... 96
Layout Examples.......................................................................................................................... 99
A Column of Settings................................................................................................................ 99
Dynamic Content ................................................................................................................... 100
A Modular User Interface........................................................................................................ 102
The Last Word............................................................................................................................ 104
Chapter 4: Dependency Properties ................................................................................................ 105
Understanding Dependency Properties ....................................................................................... 105
Defining a Dependency Property ............................................................................................ 106
Registering a Dependency Property........................................................................................ 107
Adding a Property Wrapper .................................................................................................... 109
How WPF Uses Dependency Properties .................................................................................. 110
Shared Dependency Properties .............................................................................................. 111
Attached Dependency Properties............................................................................................ 112
Property Validation ..................................................................................................................... 114
The Validation Callback .......................................................................................................... 114
The Coercion Callback ........................................................................................................... 115
The Last Word............................................................................................................................ 118
vi
Zgłoś jeśli naruszono regulamin