NE16 WPF in Windows Forms and Vice Versa

10
10/7/2008 Copyright © 2008 IDesign, Brian Noyes 1 1 Windows Presentation Foundation In Windows Forms And Vice Versa Brian Noyes Chief Architect IDesign Inc (www.idesign.net ) 2 About Brian Chief Architect, IDesign Inc. (www.idesign.net ) Microsoft Regional Director/MVP Writing Developing Applications with Windows Workflow Foundation, LiveLessons, June 2007 Smart Client Deployment with ClickOnce, Addison-Wesley, Jan 2007 Data Binding in Windows Forms 2.0, Addison-Wesley, January 2006 MSDN Magazine, MSDN Online, CoDe Magazine, TheServerSide.NET, asp.netPRO, Visual Studio Magazine Speaking Microsoft TechEd, Visual Studio Connections, DevTeach, DevReach, INETA Speakers Bureau, MSDN Webcasts Participates in Microsoft Design Reviews E-mail: [email protected] Blog: http://briannoyes.net

Transcript of NE16 WPF in Windows Forms and Vice Versa

Page 1: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 1

1

Windows Presentation Foundation In Windows Forms And Vice Versa

Brian NoyesChief ArchitectIDesign Inc (www.idesign.net)

2

About Brian• Chief Architect, IDesign Inc. (www.idesign.net) • Microsoft Regional Director/MVP• Writing

– Developing Applications with Windows Workflow Foundation, LiveLessons, June 2007

– Smart Client Deployment with ClickOnce, Addison-Wesley, Jan 2007– Data Binding in Windows Forms 2.0, Addison-Wesley, January 2006– MSDN Magazine, MSDN Online, CoDe Magazine, TheServerSide.NET,

asp.netPRO, Visual Studio Magazine• Speaking

– Microsoft TechEd, Visual Studio Connections, DevTeach, DevReach, INETA Speakers Bureau, MSDN Webcasts

• Participates in Microsoft Design Reviews• E-mail: [email protected]• Blog: http://briannoyes.net

Page 2: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 2

3

WPF/Windows Forms Interoperability Overview

Hosting WPF Controls in Windows Forms

Hosting Windows Forms Controls in WPF

Interoperability Challenges

Using Property Maps

Visual Studio 2008 Design-Time Support

Agenda

4

Why Interop?• WPF adoption is not all or nothing

– Leverage existing investment in Windows Forms controls or applications

– Use the capabilities of WPF incrementally as needed

• WPF design tools are not fully mature– Microsoft Visual Studio 2008 tools are v1.0– Microsoft Expression Blend good for certain tasks,

but not a full blown coder’s tool

Page 3: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 3

5

Why Interop?• Windows Forms control suite is more comprehensive than

WPF (at this point in time)– DataGridView/DataGrid

• Xceed / Infragistics/etc. options– BindingNavigator– ColorDialog– DateTimePicker– ErrorProvider– FontDialog– FolderBrowserDialog– MaskedTextBox– etc.

6

Interop Overview• Can easily host

– WPF controls in Windows Forms– Windows Forms controls in WPF

• Direction choice– WPF in Windows Forms

• Existing Windows Forms app that you are adding new graphics capabilities to

– Windows Forms in WPF• Leverage existing investment in Windows Forms

controls or compensate for missing controls in WPF

Page 4: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 4

7

Interop Fundamentals• WPF and Windows Forms controls and components are

still just .NET class instances– References to objects on the heap

• Can use non-visual components from either framework in the other– Ex: WPF Line object can be treated as a data structure

• Rendering engines are very different, so need different screen ownership model– Shared screen areas may not behave as expected

8

WPF/Windows Forms Interoperability Overview

Hosting WPF Controls in Windows Forms

Hosting Windows Forms Controls in WPF

Interop Challenges

Using Property Maps

Visual Studio 2008 Design-Time Support

Agenda

Page 5: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 5

9

Hosting WPF Controls In Windows Forms

• ElementHost– Windows Forms control that

hosts a single WPF Element• Typically a composite WPF control

– Provides rendering container for hosted WPF element within a Form/Control

– Hosting Windows Forms Control in WPF– Exposes properties to control the

presentation of the hosted element within the ElementHost container

10

Hosting WPF Controls In Windows Forms

• Create or obtain the WPF control• Create the Windows Forms host application• Add reference to the WindowsFormsIntegration assembly• Create an ElementHost control in the Windows Form• Create instance of WPF control• Set the WPF control as the Child of the ElementHost• Add the host to the Controls collectionfor the container

– Form, user control, or composite control

Page 6: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 6

11

WPF/Windows Forms Interoperability Overview

Hosting WPF Controls in Windows Forms

Hosting Windows Forms Controls in WPF

Interoperability Challenges

Using Property Maps

Visual Studio 2008 Design-Time Support

Agenda

12

Hosting Windows Forms Control in WPF

• WindowsFormsHost– WPF Element that hosts a single Windows Forms

control– Provides rendering container for Windows Forms

within WPF Window/Control/Page– System.Windows.Forms.Integration namespace– Exposes properties to control the presentation of the

hosted control within the WindowsFormsHost container

Page 7: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 7

13

Hosting Windows Forms Control In WPF

• Create or obtain the Windows Forms control• Create the WPF host application• Add reference to the WindowsFormsIntegration assembly• Add WindowsFormsHost control to window/page• Create instance of WinForms control• Set as Child of WindowsFormsHost• Add the host to the Children collection

– Implicit in XAML based on the element hierarchy

14

WPF/Windows Forms Interoperability Overview

Hosting WPF Controls in Windows Forms

Hosting Windows Forms Controls in WPF

Interoperability Challenges

Using Property Maps

Visual Studio 2008 Design-Time Support

Agenda

Page 8: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 8

15

Interop Challenges

• Focus• Styling• Overlap• Coordinates• Windows Forms

Instancing in Pages• Opacity

16

WPF/Windows Forms Interoperability Overview

Hosting WPF Controls in Windows Forms

Hosting Windows Forms Controls in WPF

Interoperability Challenges

Using Property Maps

Visual Studio 2008 Design-Time Support

Agenda

Page 9: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 9

17

Property Maps• Map changes in host properties to

changes in hosted control/element– Example: Background in WPF ->

BackColor in Windows Forms• Automatic detection and value passing

– Only works for certain properties• Can add custom handling of mapping process

– host.PropertyMap.Add()– Delegate pointing to method with signature

• void MethodName(object hostObj, string propName, object valueBeingSet)

18

WPF/Windows Forms Interoperability Overview

Hosting WPF Controls in Windows Forms

Hosting Windows Forms Controls in WPF

Interoperability Challenges

Using Property Maps

Visual Studio 2008 Design-Time Support

Agenda

Page 10: NE16 WPF in Windows Forms and Vice Versa

10/7/2008

Copyright © 2008 IDesign, Brian Noyes 10

19

Visual Studio Design-Time Support For Interop• Add WPF controls and element

host from designer– Toolbox item drag and drop– Some ElementHost properties

settable from designer– Contained control properties and

events not accessible from designer• Drop to code

• Windows Forms to WPF support – None

20

ResourcesWindows Presentation Foundation Unleashed, Windows Presentation Foundation Unleashed, Adam Nathan, Adam Nathan, SamsSams PublishingPublishing

Programming WPF, Programming WPF, Chris Sells and Ian Griffiths, AddisonChris Sells and Ian Griffiths, Addison--WesleyWesley

Windows Forms 2.0 Programming, Windows Forms 2.0 Programming, Chris Sells and Michael Chris Sells and Michael WeinhardtWeinhardt, Addison, Addison--WesleyWesley

Essential Windows Presentation Foundation, Essential Windows Presentation Foundation, Chris Anderson, AddisonChris Anderson, Addison--WesleyWesley