Vous êtes sur la page 1sur 19

Visual Studio 2008: Windows Presentation Foundation

Module 6: Creating New Controls


Overview of Control Authoring Creating Controls

Lesson: Overview of Control Authoring


Why Create New Controls? Options for Creating New Controls User Controls Custo !ra

Controls

ewor"#le ent$%erived Controls

Why Create New Controls?


WPF provides many features that reduce the need to create new controls:
&i'h 'ontent (tyles Control te plates )riggers %ata te plates

Options for Creating New Controls


WPF provides three control-authoring models:
%eriving fro %eriving fro %eriving fro the UserControl 'lass the Control 'lass the FrameworkElement 'lass

UserControl

Control

FrameworkElement

User Controls
To create a user control:
%efine a UserControl ele ent *y using +AML %efine a 'lass that inherits fro UserControl Use styles and triggers if re,uired

Consider creating a user control when:


-ou want to *uild a 'ontrol in a si ilar *uild an appli'ation anner to how you

-our 'ontrol 'onsists only of e.isting 'o ponents -ou do not need to support 'o ple. 'usto i/ation

Custo

Controls

To create a custom control:


%efine a 'lass that inherits fro Use the es if re,uired Control %efine a ControlTemplate ele ent

Consider creating a custom control when:


-ou want to ena*le 'usto i/ation of your 'ontrol *y using 'ontrol te plates -ou want your 'ontrol to support various the es

!ra ewor"#le ent$%erived Controls


There are two methods to create a FrameworkElementderived control:
%ire't rendering Custo ele ent 'o position

Consider using a FrameworkElement-derived control when:


-ou want pre'ise 'ontrol over the appearan'e -ou want to use your own rendering logi' -ou want to use 'usto ele ent 'o position

Lesson: Creating Controls


Creating a User Control 0

ple enting 1roperties and #vents Control ands es

Creating a Custo 0

ple enting Co

#nhan'ing Controls *y Using )he %e

onstration: 0 ple enting a NumericUpDown Control

Creating a User Control


To implement the UI of a user control:
Create a UserControl +AML ele ent Add layout ele ents and standard 'ontrols 0 ple ent a 'lass that inherits fro UserControl

<UserControl x:Class="MyNamespace.NumericUpDown" ...> <Gri ...> <Text!lock ..."> <#epeat!utton ...>Up<"#epeat!utton> namespace $%Namespace & pu'lic class NumericUpDown : UserControl & ...

0 ple enting 1roperties and #vents


To define properties and events for a user control:
%efine properties as dependen'y properties %efine events as routed events

pu'lic static rea onl% DependencyProperty (alue)ropert% = Depen enc%)ropert%.Register*"(alue"+ ...,pu'lic ecimal (alue & .et & return * ecimal,GetValue*(alue)ropert%,- / set & SetValue*(alue)ropert%+ 0alue,- / / pu'lic static rea onl% RoutedE ent (alueC1an.e E0ent = E0ent$ana.er.RegisterRoutedE ent*"(alueC1an.e "+ ...,-

Creating a Custo

Control

To define a custom control:


Create a 'lass that inherits fro the Control 'lass %efine the appearan'e *y using a 4t%le ele ent

namespace $%Namespace & pu'lic class NumericUpDown : Control &.../ ... <2pplication xmlns:local="clr3namespace:MyNamespace" ...> <2pplication.#esources> ... <ControlTemplate Tar.etT%pe="&x:T%pe local:NumericUpDown/"> <Gri > ...

0 ple enting Co

ands
'ontrols to de'ouple

-ou i ple ent 'o ands in 'usto the event handling for the 'ontrol

<#epeat!utton Comman ="&x:4tatic local$NumericUpDown.!ncreaseCommand/" ...>Up<"#epeat!utton> <#epeat!utton Comman ="&x:4tatic local$NumericUpDown.DecreaseCommand/" ...>Down<"#epeat!utton>

pu'lic class NumericUpDown : Control & pu'lic static RoutedCommand !ncreaseCommandpu'lic static RoutedCommand"DecreaseCommand# ... %efined in the te plate of a 4t%le ele ent

#nhan'ing Controls *y Using )he es


To create a theme file:
Create a folder na ed the es Create generi'2.a l %efine a #esourceDictionar% with the 4t%le ele ent (pe'ify the the e lo'ation in the hosting appli'ation

<#esourceDictionar% ...> <4t%le Tar.etT%pe="&x:T%pe local:NumericUpDown/"> <ControlTemplate Tar.etT%pe="&x:T%pe .../"> ... 5assem'l%: T1eme6n7o* #esourceDictionar%8ocation.None+ #esourceDictionar%8ocation.4ource2ssem'l%,9 %efined in generi'2.a l

0n the hosting appli'ation

%e onstration: 0 ple enting a Nu eri'Up%own Control


0n this de onstration3 you will see how to:
0 0

ple ent a user 'ontrol ple ent a 'usto 'ontrol

Notes 1age Over$flow (lide2 %o Not 1rint (lide2 (ee Notes pane2

La*: #nhan'ing User 0nterfa'es *y Using Custo Controls


#.er'ise 4: 0

ple enting a Custo

Control

Logon infor ation


5irtual a'hine 6667A$LON$%#5$76 (tudent 1a88w7rd
! minutes

User na e 1assword
Estimated time:

La* &eview
9ow do you i

ple ent a 'usto

'ontrol? 'ontrol?

9ow do you define the appearan'e of a 'usto

Module &eview and )a"eaways


&eview :uestions ;est 1ra'ti'es

Vous aimerez peut-être aussi