6888653-6460a06
TRANSCRIPT
-
8/13/2019 6888653-6460A06
1/19
Visual Studio2008:
WindowsPresentation
Foundation
-
8/13/2019 6888653-6460A06
2/19
Module 6: Creating New Controls
Overview of Control Authoring
Creating Controls
-
8/13/2019 6888653-6460A06
3/19
Lesson: Overview of Control Authoring
Why Create New Controls?
Options for Creating New Controls
User Controls
Custo Controls
!raewor"#leent$%erived Controls
-
8/13/2019 6888653-6460A06
4/19
Why Create New Controls?
WPF provides many features that reduce the need to
create new controls:
&i'h 'ontent
(tyles
Control teplates
)riggers
%ata teplates
-
8/13/2019 6888653-6460A06
5/19
Options for Creating New Controls
WPF provides three control-authoring models:
%eriving fro the UserControl'lass
%eriving fro the Control'lass
%eriving fro the FrameworkElement'lass
FrameworkElementControlUserControl
-
8/13/2019 6888653-6460A06
6/19
User Controls
To create a user control:
%efine a UserControleleent *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 siilar anner to how you
*uild an appli'ation -our 'ontrol 'onsists only of e.isting 'oponents
-ou do not need to support 'ople. 'ustoi/ation
-
8/13/2019 6888653-6460A06
7/19
Custo Controls
To create a custom control:
%efine a 'lass that inherits fro Control
%efine a ControlTemplateeleent
Use thees if re,uired
Consider creating a custom control when:
-ou want to ena*le 'ustoi/ation of your 'ontrol *y using
'ontrol teplates -ou want your 'ontrol to support various thees
-
8/13/2019 6888653-6460A06
8/19
!raewor"#leent$%erived Controls
There are two methods to create a FrameworkElement-
derived control:
%ire't rendering
Custo eleent 'oposition
Consider using a FrameworkElement-derived controlwhen:
-ou want pre'ise 'ontrol over the appearan'e
-ou want to use your own rendering logi'
-ou want to use 'usto eleent 'oposition
-
8/13/2019 6888653-6460A06
9/19
Lesson: Creating Controls
Creating a User Control
0pleenting 1roperties and #vents
Creating a Custo Control
0pleenting Coands
#nhan'ing Controls *y Using )hees %eonstration: 0pleenting a NumericUpDownControl
-
8/13/2019 6888653-6460A06
10/19
Creating a User Control
Up
namespace $%Namespace
&
pu'lic class NumericUpDown: UserControl
&
...
To implement the UI of a user control:
Create a UserControl+AML eleent
Add layout eleents and standard 'ontrols
0pleent a 'lass that inherits fro UserControl
-
8/13/2019 6888653-6460A06
11/19
0pleenting 1roperties and #vents
pu'lic static reaonl% DependencyProperty(alue)ropert%= Depenenc%)ropert%.Register*"(alue"+ ...,-
pu'lic ecimal (alue
&
et & return *ecimal,GetValue*(alue)ropert%,- /
set & SetValue*(alue)ropert%+ 0alue,- //
pu'lic static reaonl% RoutedEent(alueC1aneE0ent
= E0ent$anaer.RegisterRoutedEent*"(alueC1ane"+ ...,-
To define properties and events for a user control:
%efine properties as dependen'y properties
%efine events as routed events
-
8/13/2019 6888653-6460A06
12/19
Creating a Custo Control
...
...
namespace $%Namespace&
pu'lic class NumericUpDown: Control&.../
...
To define a custom control:
Create a 'lass that inherits fro the Control 'lass
%efine the appearan'e *y using a 4t%leeleent
-
8/13/2019 6888653-6460A06
13/19
0pleenting Coands
pu'lic class NumericUpDown : Control
&
pu'lic static RoutedCommand!ncreaseCommand- pu'lic static RoutedCommand DecreaseCommand#
...
Up
Down
-ou ipleent 'oands in 'usto 'ontrols to de'ouplethe event handling for the 'ontrol
%efined in the teplate ofa4t%leeleent
-
8/13/2019 6888653-6460A06
14/19
#nhan'ing Controls *y Using )hees
...
5assem'l%: T1eme6n7o*
#esourceDictionar%8ocation.None+#esourceDictionar%8ocation.4ource2ssem'l%,9
To create a theme file:
Create a folder naed thees
Create generi'2.al
%efine a #esourceDictionar%with the 4t%le eleent
(pe'ify the thee lo'ation in the hosting appli'ation
%efined in generi'2.al
0n the hosting appli'ation
% i 0 l i N i U %
-
8/13/2019 6888653-6460A06
15/19
%eonstration: 0pleenting a Nueri'Up%ownControl
0n this deonstration3 you will see how to:
0pleent a user 'ontrol
0pleent a 'usto 'ontrol
N t 1 O fl (lid % N t 1 i t (lid
-
8/13/2019 6888653-6460A06
16/19
Notes 1age Over$flow (lide2 %o Not 1rint (lide2(ee Notes pane2
L * # h i U 0 t f * U i
-
8/13/2019 6888653-6460A06
17/19
La*: #nhan'ing User 0nterfa'es *y UsingCusto Controls
#.er'ise 4: 0pleenting a Custo Control
Logon inforation
5irtual a'hine 667A$LON$%#5$76
User nae (tudent
1assword 1a88w7rd
Estimated time: ! minutes
-
8/13/2019 6888653-6460A06
18/19
La* &eview
9ow do you ipleent a 'usto 'ontrol?
9ow do you define the appearan'e of a 'usto 'ontrol?
-
8/13/2019 6888653-6460A06
19/19
Module &eview and )a"eaways
&eview uestions
;est 1ra'ti'es