ebook img

Pro WPF in C# 2008: Windows Presentation Foundation with .NET 3.5 PDF

1072 Pages·2008·37.48 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro WPF in C# 2008: Windows Presentation Foundation with .NET 3.5

cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in .neT Companion eBook Available Pro WPF in C# 2008: Windows Presentation Pro Pro Foundation with .NET 3.5, SECoNd EdiTioN Dear Reader, For years, .NET developers have created Windows applications with the W WPF Windows Forms toolkit. And though Windows Forms is mature and reasonably full-featured, it’s hardwired to essential bits of Windows plumbing that haven’t changed much in the past ten years. As a result, some tasks that should be easy Author of require a lot of work. in Pro .NET 2.0 Windows The Windows Presentation Foundation (WPF) changes all this by introduc- Forms and Custom Controls ing a new model with entirely different plumbing. Although WPF includes the (Apress) standardcontrolsyou’refamiliarwith,itdrawseverydetailitself,usingthe Pro .NET 2.0 Windows high-performanceDirectXengine.Asaresult,WPFcanprovideawholehost P Forms and Custom Controls of eye-popping new features, all of which we’ll cover in this book. For example, in VB 2005 (Apress) we’ll give common controls like buttons a slick, customized look without writ- C# 2008 Pro ASP.NET 3.5 in C# 2008 ingalineofcode.Similarly,wewillcraftanimatedeffectsthatrotate,stretch, (Apress,withMarioSzpuszta) scale,andskewanythinginyouruserinterfacerightbeforetheuser’seyes. Pro WPF:Windows Tasks that would have been difficult or nearly impossible in Windows Forms— F Presentation Foundation such as playing media files or creating texture-mapped 3-D shapes—are finally in .NET 3.0 (Apress) possible. By the time you’ve finished this book, you’ll have mastered these pre- viously impossible tasks. Although WPF’s video, animation, and 3-D features get the most hype, WPF is just as useful for building business applications that don’t need rich graphics. We’ll cover WPF’s enhancements of key business features such as data binding, printmanagement,anddocumentdisplay,givingyouallyouneedtocreate C# professional business applications in WPF. in Windows Presentation Foundation with .NET 3.5 Welcome aboard! Matthew MacDonald (Microsoft MVP, MCSD) 2008 Create the next generation of Windows applications. Companion eBook THE APRESS ROADMAP Beginning C# 2008 Databases Pro C# 2008 and the .NET Pro WPF in C# 2008, Beginning C# 2008 3.5Platform,Fourth Edition Second Edition SECoND EDiTioN Beginning VB 2008 Databases See last page for details Pro ASP.NET 3.5 in Illustrated C# 2008 Pro LINQ on $10 eBook version C# 2008, Second Edition Beginning SECoNd EdiTioN XML with C# 2008 Matthew MacDonald SOURCE CODE ONLINE ISBN-13: 978-1-59059-955-6 www.apress.com ISBN-10: 1-59059-955-1 55499 MacDonald US $54.99 Shelve in Programming/Microsoft/.NET User level: 9 781590 599556 Intermediate–Advanced this print for content only—size & color not accurate spine = 2.005" 1,072 page count 9551CH00 2/8/08 1:40 PM Page i Pro WPF in C# 2008 Windows Presentation Foundation with .NET 3.5 SECOND EDITION Matthew MacDonald 9551CH00 2/8/08 1:40 PM Page ii Pro WPF in C# 2008: Windows Presentation Foundation with .NET 3.5, Second Edition Copyright © 2008 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-0576-0 ISBN-10 (pbk): 1-59059- 955-1 ISBN-13 (electronic): 978-1-59059-955-6 ISBN-10 (electronic): 1-4302-0576-8 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. Lead Editor: Matt Moodie Technical Reviewer: Christophe Nasarre Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Sofia Marchant Copy Editor: Kim Wimpsett Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Diana Van Winkle Proofreader: Nancy Sixsmith Indexer: Broccoli Information Management Artists: Diana Van Winkle, April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski 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 [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://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 http://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 http://www.apress.com. 9551CH00 2/8/08 1:40 PM Page iii For my wonderful family, Faria and Maya 9551CH00 2/8/08 1:40 PM Page iv 9551CH00 2/8/08 1:40 PM Page v Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv ■ CHAPTER 1 Introducing WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ■ CHAPTER 3 The Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 ■ CHAPTER 4 Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 ■ CHAPTER 5 Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 ■ CHAPTER 6 Dependency Properties and Routed Events . . . . . . . . . . . . . . . . . . . . . . 137 ■ CHAPTER 7 Classic Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 ■ CHAPTER 8 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 ■ CHAPTER 9 Pages and Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 ■ CHAPTER 10 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 ■ CHAPTER 11 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 ■ CHAPTER 12 Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 ■ CHAPTER 13 Shapes, Transforms, and Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 ■ CHAPTER 14 Geometries, Drawings, and Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 ■ CHAPTER 15 Control Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 ■ CHAPTER 16 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 ■ CHAPTER 17 Data Templates, Data Views, and Data Providers . . . . . . . . . . . . . . . . . 551 ■ CHAPTER 18 Lists, Trees, Toolbars, and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 ■ CHAPTER 19 Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 ■ CHAPTER 20 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697 ■ CHAPTER 21 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729 ■ CHAPTER 22 Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783 ■ CHAPTER 23 3-D Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809 ■ CHAPTER 24 Custom Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855 ■ CHAPTER 25 Interacting with Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903 ■ CHAPTER 26 Multithreading and Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927 ■ CHAPTER 27 ClickOnce Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965 ■ INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983 v 9551CH00 2/8/08 1:40 PM Page vi 9551CH00 2/8/08 1:40 PM Page vii Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv ■ CHAPTER 1 Introducing WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Understanding Windows Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 DirectX: The New Graphics Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Hardware Acceleration and WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 WPF: A Higher-Level API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Resolution Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The Evolution of WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Windows Forms Lives On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 DirectX Also Lives On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 The Architecture of WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 ■ CHAPTER 2 XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Understanding XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Graphical User Interfaces Before WPF . . . . . . . . . . . . . . . . . . . . . . . . 22 The Variants of XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 XAML Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 XAML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 XAML Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The Code-Behind Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Properties and Events in XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Simple Properties and Type Converters . . . . . . . . . . . . . . . . . . . . . . . 31 Complex Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Markup Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Attached Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Nesting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Special Characters and Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . 40 vii 9551CH00 2/8/08 1:40 PM Page viii viii ■ CONTENTS Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Full Eight Ball Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Using Types from Other Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Loading and Compiling XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Code-Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Code and Uncompiled XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Code and Compiled XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 XAML Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 ■ CHAPTER 3 The Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 The Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Creating an Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Deriving a Custom Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Application Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Application Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Handling Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 64 Accessing the Current Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Interacting Between Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Single-Instance Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 ■ CHAPTER 4 Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Understanding Layout in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The WPF Layout Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 The Layout Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 The Layout Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Simple Layout with the StackPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Layout Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Margin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Minimum, Maximum, and Explicit Sizes . . . . . . . . . . . . . . . . . . . . . . . 84 The WrapPanel and DockPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 The WrapPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 The DockPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Nesting Layout Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 The Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Fine-Tuning Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Spanning Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Split Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Description:
The Microsoft Windows Presentation Foundation provides the foundation for building applications and high–quality user experiences in Windows Vista. WPF blends application user interface, documents, and media content to provide richer control, design, and development of the visual aspects of Window
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.