www.it-ebooks.info Professional WPF Programming: .NET Development with the Windows ® Presentation Foundation Chris Andrade, Shawn Livermore, Mike Meyers, Scott Van Vliet 01_041802 ffirs.qxp 4/2/07 5:16 PM Page iii www.it-ebooks.info 01_041802 ffirs.qxp 4/2/07 5:16 PM Page ii www.it-ebooks.info Professional WPF Programming: .NET Development with the Windows ® Presentation Foundation 01_041802 ffirs.qxp 4/2/07 5:16 PM Page i www.it-ebooks.info 01_041802 ffirs.qxp 4/2/07 5:16 PM Page ii www.it-ebooks.info Professional WPF Programming: .NET Development with the Windows ® Presentation Foundation Chris Andrade, Shawn Livermore, Mike Meyers, Scott Van Vliet 01_041802 ffirs.qxp 4/2/07 5:16 PM Page iii www.it-ebooks.info Professional WPF Programming: .NET Development with the Windows ® Presentation Foundation Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-04180-2 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication Data is available from the publisher. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permis- sion of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley .com/go/permissions . LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HERE- FROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAP- PEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. 01_041802 ffirs.qxp 4/2/07 5:16 PM Page iv www.it-ebooks.info About the Authors Chris Andrade is a Principal with Novera Consulting, a Microsoft Certified Partner specializing in enterprise architecture and development with .NET and Microsoft server technology. Chris devotes most of his time to assisting companies in applying Microsoft technologies to improve their business pro- cesses and operations. Chris has worked within a diverse range of business verticals, including automo- tive, healthcare, and mortgage. Chris also takes active participation within the local developer community, speaking and presenting along the west coast whenever possible. Shawn Livermore (MCAD, MCSD, PMP) [shawnlivermore.blogspot.com] has been architecting and developing Microsoft-based solutions for nearly a decade. Shawn has been consulting as an enterprise and solutions architect for Fortune 500 clientele within highly visible enterprise implementations. His range of technical competence stretches across platforms and lines of business, but he specializes in Microsoft .NET enterprise application architectures and Microsoft server-based product integrations. Shawn lives in the Southern California area with his beautiful wife Shantell and amazing daughter Elexzandreia. Shawn also enjoys beating his friend Jason at air hockey, basketball, baseball, football, arcade games, cards, billiards, ping pong, shuffleboard, trivia, golf, racquetball, dirt bike races, cross- country skiing and pretty much any other sport in existence. Shawn would like to once again remind Jason, “Who’s your daddy?” Mike Meyers is president of Novera Consulting Inc, a software development and consulting firm spe- cializing in custom application development utilizing the .NET platform and Microsoft server technol- ogy. Based in Orange County, California, Mike’s company is focused on providing solutions based on Microsoft platforms and technology to companies spanning multiple industries. Mike has worked in a number of industries, including mortgage, healthcare, and various ecommerce ventures as developer, architect, and project manager. When he’s not writing code, Mike is active mountain biking in southern California, hiking with his four-legged Labrador Dakota or playing music with friends. Scott Van Vliet is an accomplished Solutions Architect who has spent the past decade delivering suc- cessful Microsoft-based solutions to his clients. Currently a Senior Manager with Capgemini, a world- wide leader in technology consulting services, Scott has managed, architected, and developed solutions for companies across the globe. He is also the Microsoft Delivery Leader for Capgemini’s Telecom, Media & Entertainment practice, providing technical and engagement leadership on all Microsoft-based projects. Scott can be reached via his Web site at http://www.scottvanvliet.com/. 01_041802 ffirs.qxp 4/2/07 5:16 PM Page v www.it-ebooks.info To my wife Sena and son Ethan: for their patience, love, and understanding during the process of writing this book. I love you both so much. —Chris Andrade To my best friend and life companion, Shantell, who has stuck around with me through it all and never once doubted me. You’ve believed in me from the beginning when we had nothing. I appreciate your friendship and your love, and look forward to getting old and wrinkled together. Thank you for supporting this book and the time it required. And, of course, I am so thankful for my daughter Elexzandreia, who has taught me so many things. Thanks for letting daddy type on the laptop while you write me little cards and “projects.” We must have gone through thousands of tape dispensers and construction paper packages over these last few years. You’re an angel, and my greatest accomplishment in this life would be to see your life flourish and grow into the woman God has designed you to be. —Shawn Livermore To my parents, Richard and Linda, who have always supported my entrepreneurial spirit, helping me to become who I am today. In particular, I dedicate this to my father for spending 30 years with I.B.M. to support his family and foster my interest in computers, and to my mother for raising a handful and teaching me an appreciation for writing and the arts. I would also like to dedicate this book to my brother Nick. No one could ask for a better brother or friend. —Mike Meyers 01_041802 ffirs.qxp 4/2/07 5:16 PM Page vi www.it-ebooks.info Credits Senior Acquisitions Editor Jim Minatel Development Editor John Sleeva Technical Editor Carl Daniel Production Editor Christine O’Connor Copy Editor Nancy Rapoport Editorial Manager Mary Beth Wakefield Production Manager Tim Tate Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert Graphics and Production Specialists Brooke Graczyk Denny Hager Jennifer Mayberry Barbara Moore Alicia B. South Quality Control Technicians Cynthia Fields Brian H. Walls Project Coordinator Adrienne Martinez Proofreading and Indexing Aptara Anniversary Logo Design Richard Pacifico 01_041802 ffirs.qxp 4/2/07 5:16 PM Page vii www.it-ebooks.info [...]... Chapter 10: WPF and Win32 Interop 329 Win32 User Interface Overview How WPF and HWNDs Interoperate Using Win32 HWNDs Inside of WPF 330 330 331 Hosting a Win32 Button in WPF 332 Using WPF in Win32 Applications Adding Windows Forms Controls to WPF 335 337 Adding Your WindowsFormsHost in Code Adding Your HwndHost in XAML Adding ActiveX Controls to WPF Adding the ActiveX Control in XAML Adding WPF Controls... chapters, as interest or needs dictate, should be fine What This Book Covers Professional WPF Programming provides a developer-level tutorial of WPF programming techniques, patterns, and use cases The book begins with an overview of WPF Questions such as “What is WPF? ”, “How do I start using WPF? ” and “What does WPF have to offer me?” are answered early in the first chapter, followed swiftly by a detailed... 23 Chapter 2: WPF and NET Programming Getting Started 25 26 Required Installations Types of WPF Applications My First WPF Application 26 26 27 WPF Development Concepts 29 XAML The Application Object Creating the User Interface Handling Events Working with Controls Triggers Language Support Deployment 29 30 32 35 41 51 58 61 Summary 61 www.it-ebooks.info Contents Chapter 3: Anatomy of a WPF- Enabled Application... you dive right into the programming techniques you need for WPF Using Visual Studio to create some examples, you get up-to-speed fast and start creating applications right away Also, you are introduced to XAML, the new markup syntax for creating a UI XAML is actually a separate technology from WPF, but WPF uses it extensively With the basic concepts and programming techniques of WPF under your belt, you... concepts One of those concepts is how to mix Win32 and WPF code Interoperation considerations will be covered Hosting Win32 in WPF and the reverse is possible, and you’ll learn how to implement this feature so that you can start incorporating WPF into your Win32 applications right away The book finishes with a WPF deep dive into architecture, WPF framework, XAML, and threading considerations In addition,... you will build a sample WCF service and a simple WF workflow application How This Book Is Structured Professional WPF Programming begins by providing some background on how and why Microsoft is shifting away from the long-standing Win32 platform in the direction of WPF This is followed by an overview of WPF and the core components and subsystems of the platform Next, a tour of Expression Blend is offered,... how this paved the way for a new platform, WPF You will then take a high-level tour of the WPF architecture, including the new graphics capabilities and subsystems that are introduced by the platform Additional development tools are also introduced ❑ Chapter 2, WPF and NET Programming —This chapter walks you through the requirements for building and running WPF applications You also learn about the... by WPF applications, and will be noted where appropriate Element Trees An important concept to grasp in WPF is the notion of element trees, which represent the visual elements of which a WPF application is comprised Two element trees exist within a WPF application: the logical tree and the visual tree Logical Tree The logical tree is the hierarchical structure containing the exact elements of your WPF. .. developer to code against in Visual Studio using WPF This book covers the concepts and components that make up the Windows Presentation Foundation You learn how to create a rich UI, exploring the various controls now available to you, and how to leverage the new content model that WPF provides You explore the WPF object model as well as the new subsystems offered in WPF, such as the dependency property system... 8, “Using WPF in the Enterprise”—This chapter explores the many factors to consider when building an enterprise-level application with WPF Application models, deployment, hosting environment, security considerations, navigation, state management, and localization are covered thoroughly in this chapter ❑ Chapter 9, “Security”—This chapter explores the security features of WPF Security in WPF is primarily . 23 Chapter 2: WPF and .NET Programming 25 Getting Started 26 Required Installations 26 Types of WPF Applications 26 My First WPF Application 27 WPF Development. iii www.it-ebooks.info 01_041802 ffirs.qxp 4/2/07 5:16 PM Page ii www.it-ebooks.info Professional WPF Programming: .NET Development with the Windows ® Presentation Foundation 01_041802