ptg
ptg
WPF 4
UNLEASHED
800 East 96th Street, Indianapolis, Indiana 46240 USA
Adam Nathan
ptg
WPF 4 Unleashed
Copyright © 2010 by Pearson Education
All rights reserved. No part of this book shall be reproduced, stored in a retrieval
system, or transmitted by any means, electronic, mechanical, photocopying, recording,
or otherwise, without written permission from the publisher. No patent liability is
assumed with respect to the use of the information contained herein. Although every
precaution has been taken in the preparation of this book, the publisher and author
assume no responsibility for errors or omissions. Nor is any liability assumed for
damages resulting from the use of the information contained herein.
ISBN-13: 978-0-672-33119-0
ISBN-10: 0-672-33119-5
Library of Congress Cataloging-in-Publication Data
Nathan, Adam.
WPF 4 unleashed / Adam Nathan.
p. cm.
Includes index.
ISBN 978-0-672-33119-0
1. Windows presentation foundation. 2. Application software. 3. Microsoft .NET
Framework. I. Title.
QA76.76.A65N386 2010
006.7’882—dc22
2010017765
Printed in the United States on America
First Printing June 2010
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks
have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of
this information. Use of a term in this book should not be regarded as affecting the
validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possi-
ble, but no warranty or fitness is implied. The information provided is on an “as is”
basis. The author(s) and the publisher shall have neither liability nor responsibility to
any person or entity with respect to any loss or damages arising from the information
contained in this book or from the use of the programs accompanying it.
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quantity for
bulk purchases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearsoned.com
Editor-in-Chief
Karen Gettman
Executive Editor
Neil Rowe
Development Editor
Mark Renfrow
Managing Editor
Kristy Hart
Project Editor
Betsy Harris
Copy Editor
Kitty Wilson
Indexer
Erika Millen
Proofreader
Kathy Ruiz
Technical Editors
Dwayne Need
Robert Hogue
Joe Castro
Jordan Parker
Publishing Coordinator
Cindy Teeters
Book Designer
Gary Adair
Composition
Bronkella Publishing LLC
From the Library of Wow! eBook
ptg
Contents at a Glance
Introduction . .................................................................................................................................................. 1
Part I Background
1 Why WPF, and What About Silverlight? ................................................................................. 9
2 XAML Demystified . ................................................................................................................................ 21
3 WPF Fundamentals . .............................................................................................................................. 73
Part II Building a WPF Application
4 Sizing, Positioning, and Transforming Elements . .......................................................... 97
5 Layout with Panels .............................................................................................................................. 115
6 Input Events: Keyboard, Mouse, Stylus, and Multi-Touch .................................. 159
7 Structuring and Deploying an Application ...................................................................... 195
8 Exploiting Windows 7. ..................................................................................................................... 233
Part III Controls
9 Content Controls . ................................................................................................................................ 261
10 Items Controls . ....................................................................................................................................... 275
11 Images, Text, and Other Controls. .......................................................................................... 309
Part IV Features for Professional Developers
12 Resources . .................................................................................................................................................... 343
13 Data Binding . ........................................................................................................................................... 363
14 Styles, Templates, Skins, and Themes . ................................................................................. 415
Part V Rich Media
15 2D Graphics .............................................................................................................................................. 475
16 3D Graphics .............................................................................................................................................. 537
17 Animation . ................................................................................................................................................ 607
18 Audio, Video, and Speech . ............................................................................................................ 653
Part VI Advanced Topics
19 Interoperability with Non-WPF Technologies ............................................................... 675
20 User Controls and Custom Controls . ................................................................................... 721
21 Layout with Custom Panels . ....................................................................................................... 751
Index . ............................................................................................................................................................. 775
From the Library of Wow! eBook
ptg
Table of Contents
Introduction 1
Who Should Read This Book? .......................................................................................................... 2
Software Requirements . ......................................................................................................................... 3
Code Examples.............................................................................................................................................. 4
How This Book Is Organized. ............................................................................................................ 4
Part I: Background. ....................................................................................................................... 4
Part II: Building a WPF Application ............................................................................... 4
Part III: Controls ............................................................................................................................ 5
Part IV: Features for Professional Developers . ........................................................ 5
Part V: Rich Media . ..................................................................................................................... 5
Part VI: Advanced Topics. ....................................................................................................... 6
Conventions Used in This Book . ................................................................................................... 6
Part I Background
1 Why WPF, and What About Silverlight? 9
A Look at the Past . .................................................................................................................................. 10
Enter WPF . .................................................................................................................................................... 11
The Evolution of WPF. ......................................................................................................................... 14
Enhancements in WPF 3.5 and WPF 3.5 SP1 ...................................................... 15
Enhancements in WPF 4 . ................................................................................................... 16
What About Silverlight? ..................................................................................................................... 18
Summary......................................................................................................................................................... 19
2 XAML Demystified
21
XAML Defined . ......................................................................................................................................... 23
Elements and Attributes ..................................................................................................................... 24
Namespaces . ................................................................................................................................................ 26
Property Elements . .................................................................................................................................. 29
Type Converters. ....................................................................................................................................... 30
Markup Extensions. ................................................................................................................................ 32
Children of Object Elements . ......................................................................................................... 35
The Content Property ............................................................................................................ 35
Collection Items. ......................................................................................................................... 36
More Type Conversion .......................................................................................................... 38
From the Library of Wow! eBook