SlideShare ist ein Scribd-Unternehmen logo
1 von 48
Presented by G. Sharada
Who is this training for?
 Preferably, has attended Session #1 training for WPF
What I intend to do.
 Problem solution approach: use sample applications to
 showcase the practical application of the topics to be
 covered.

 Less focus on theory, more on coding.
Word of caution
 In Windows Forms, there are two ways to do everything: a
  good way and a bad way.

 In WPF, there are ten ways to do everything: two that are
  amazing, 3 that are good, 1 that is bad, and 4 that suck.

 It’s distinguishing the 2 amazing solutions which is so
  challenging. So how do we get there?
Excellent Resources on WPF
 Online Resources
    MSDN
    http://www.wpftutorial.net/
    http://drwpf.com/blog/
    http://joshsmithonwpf.wordpress.com/a-guided-tour-of-wpf/
 Books
    WPF 4 Unleashed
    Pro WPF in C# 2010
    WPF Control Development Unleashed
    WPF Recipes in C# 2008
 Case studies
    Vertigo Family.Show
    PhotoSuru
Agenda
     Dependency Properties             • 25 minutes

         Routed Events                 • 20 minutes

       Control Authoring               • 25 minutes

          Data Binding                 • 25 minutes

           Convertors                  • 20 minutes

           Validation                  • 20 minutes

  Exercise #1 – Creating a generic form control
                     Q&A
Dependency properties
 Not normal properties
 A completely new implementation of properties
 Needed to use core WPF features such as animation,
  data binding and styles.
 Although, they are designed to be consumed in the
  same way as normal properties – but they vastly differ
  in their implementation behind the scenes.
 Performance was the motivating factor
Defining a dependency property
 Dependency property can be added to only
  dependency objects
 Is defined as a static field in the associated class


public static readonly DependencyProperty
 IsSpinningProperty;
Registering a dependency property
 To use dependency property we need to register it with
  WPF
 Is performed in a static constructor for the associated
  class

IsSpinningProperty = DependencyProperty.Register(
  "IsSpinning", typeof(Boolean),
Parameters to Register()
 The property name
 The data type used by the property
 The type that owns this property
 Optionally, a FrameworkPropertyMetadata object
  with additional property settings
 Optionally, a callback that performs validation for the
  property
Adding a Property(CLR) Wrapper
 Wrap dependency property in a traditional .NET
    property.

public bool IsSpinning {
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}
Property functionality
 Resources
 Data Binding
 Styles
 Animation
 Property Value inheritance
Create and use a dependency property
Attached dependency properties
 Allows different child elements to specify unique values for
  a property that is actually defined in a parent element.

 Example: DockPanel.Dock property


<DockPanel>
  <CheckBox DockPanel.Dock="Top">
      Hello
  </CheckBox>
</DockPanel>
Custom Attached properties
public static readonly DependencyProperty
  IsBubbleSourceProperty =
  DependencyProperty.RegisterAttached( "IsBubbleSource",
  typeof(Boolean), typeof(AquariumObject), new
  FrameworkPropertyMetadata(false,
  FrameworkPropertyMetadataOptions.AffectsRender) );

public static void SetIsBubbleSource(UIElement element, Boolean
  value) { element.SetValue(IsBubbleSourceProperty, value); }

public static Boolean GetIsBubbleSource(UIElement element) {
  return (Boolean)element.GetValue(IsBubbleSourceProperty); }
Create and use a attached property
Routed Events
 Events with more travelling power
    they can tunnel down or bubble up the element tree and
     be processed by event handlers along the way


 Types
    Direct events
    Bubbling events
    Tunneling events
Tunneling and Bubbling events
Registering a Routed Event
public static readonly RoutedEvent TapEvent =
  EventManager.RegisterRoutedEvent( "Tap",
  RoutingStrategy.Bubble, typeof(RoutedEventHandler),
  typeof(MyButtonSimple));
// Provide CLR accessors for the event
public event RoutedEventHandler Tap {
  add { AddHandler(TapEvent, value); }
  remove { RemoveHandler(TapEvent, value); }
}
Attached events
 Enables elements to handle events that are declared in
 a different element

<Grid Button.Click="myButton_Click">
  <Button Name="myButton" >Click Me</Button>
</Grid>
Add Events to a User Control
Reusing existing controls
 Support for rich content
 Styles to alter appearance and behavior
 Data templates to customize how data is displayed
 Control templates to define control’s structure and
  appearance
 Triggers to dynamically change appearance and
  behavior
Creating a new control
 Deriving from a user control
   You want to build your control similarly to how you
    build an application.
   Your control consists only of existing components.
   You don't need to support complex customization.
 Deriving from Control
   You want the appearance of your control to be
    customizable via the ControlTemplate.
   You want your control to support different themes.
 Deriving from FrameworkElement (Out of scope)
Create a Custom ToolTip Style
Set the Content Property of a User Control
Create a Lookless Custom Control
Data Binding
 The process that establishes a connection between the
  application UI and business logic
 Use any object as your binding source and bind it to
  almost any target UI element
 Once a binding is established, the data in the source is
  automatically and dynamically propagated to the
  binding target, and vice versa.
Data Binding Basics
 Direction of data flow
Data Binding Basics (contd.,)
 What triggers source updates
Creating a Binding
 Specifying the Binding Source
 Specifying the Path to the Value
   <DockPanel.Resources>
    <c:MyData x:Key="myDataSource"/>
   </DockPanel.Resources>
   <Button Width="150" Height="30"
    Background="{Binding Source={StaticResource
    myDataSource}, Path=ColorName}">I am bound to be
    RED!</Button>
Create a two-way binding
Binding to collections
 A binding source object can be treated either as a
 single object of which the properties contain data or as
 a data collection of polymorphic objects that are often
 grouped together
Data templating
 How the data object is displayed visually is controlled
  primarily by data templates, data converters, and data
  triggers
Use Data Templates to Display Bound Data
Data Conversion
 Convert the source value of a binding in order to assign
  it to the target value
 For example: Convert string value to type Brush
MultiBinding Convertor
 a target property has a collection of bindings
 a custom IMultiValueConverter is used to produce a
  final value from the values of the bindings

<TextBlock Name="textBox2" DataContext="{StaticResource
  NameListData}">
  <TextBlock.Text>
        <MultiBinding Converter="{StaticResource myNameConverter}"
  ConverterParameter="FormatLastFirst">
               <Binding Path="FirstName"/>
               <Binding Path="LastName"/>
        </MultiBinding>
  </TextBlock.Text>
</TextBlock>
Use Value Converters to Convert Bound Data
Data Validation
 Associating Validation Rules with a Binding
    ExceptionValidationRule - checks for exceptions thrown
     during the update of the binding source property
    DataErrorValidationRule - object checks for errors that
     are raised by objects that implement
     the IDataErrorInfo interface
 Providing Visual Feedback
    Use Validation.HasError property
    Use Validation.ErrorTemplate property
Specify Validation Rules for a Binding
Bind to IDataErrorInfo
Exercise: Create a generic form
control
 Dependency properties for form-title, help-text
 Dependency property for IsValid
 Routed events for Submit and Cancel
 Attached property to specify Submit and Cancel
  buttons
 Attached property to specify input control(s) to be
  validated
 Data template for View mode
 Control template for Edit mode
WPF - Controls &amp; Data

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascriptambuj pathak
 
MSDN Unleashed: WPF Demystified
MSDN Unleashed: WPF DemystifiedMSDN Unleashed: WPF Demystified
MSDN Unleashed: WPF DemystifiedDave Bost
 
Accessible custom radio buttons and checkboxes
Accessible custom radio buttons and checkboxesAccessible custom radio buttons and checkboxes
Accessible custom radio buttons and checkboxesRuss Weakley
 
Forms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsForms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsManuel Lemos
 
05 html-forms
05 html-forms05 html-forms
05 html-formsPalakshya
 
Advisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptAdvisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptdominion
 
Dynamic HTML Event Model
Dynamic HTML Event ModelDynamic HTML Event Model
Dynamic HTML Event ModelReem Alattas
 
Creating an Accessible button dropdown
Creating an Accessible button dropdownCreating an Accessible button dropdown
Creating an Accessible button dropdownRuss Weakley
 
Mobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptMobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptfranksvalli
 
JavaScript Presentation Frameworks and Libraries
JavaScript Presentation Frameworks and LibrariesJavaScript Presentation Frameworks and Libraries
JavaScript Presentation Frameworks and LibrariesOleksii Prohonnyi
 
04. session 04 working withformsandframes
04. session 04   working withformsandframes04. session 04   working withformsandframes
04. session 04 working withformsandframesPhúc Đỗ
 

Was ist angesagt? (19)

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Html JavaScript and CSS
Html JavaScript and CSSHtml JavaScript and CSS
Html JavaScript and CSS
 
MSDN Unleashed: WPF Demystified
MSDN Unleashed: WPF DemystifiedMSDN Unleashed: WPF Demystified
MSDN Unleashed: WPF Demystified
 
Accessible custom radio buttons and checkboxes
Accessible custom radio buttons and checkboxesAccessible custom radio buttons and checkboxes
Accessible custom radio buttons and checkboxes
 
Forms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsForms With Ajax And Advanced Plugins
Forms With Ajax And Advanced Plugins
 
Java script
Java scriptJava script
Java script
 
05 html-forms
05 html-forms05 html-forms
05 html-forms
 
XAML and WPF - Dinko Jakovljević
XAML and WPF - Dinko JakovljevićXAML and WPF - Dinko Jakovljević
XAML and WPF - Dinko Jakovljević
 
Advisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptAdvisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScript
 
WPF Controls
WPF ControlsWPF Controls
WPF Controls
 
Dynamic HTML Event Model
Dynamic HTML Event ModelDynamic HTML Event Model
Dynamic HTML Event Model
 
Sightly - Part 2
Sightly - Part 2Sightly - Part 2
Sightly - Part 2
 
Creating an Accessible button dropdown
Creating an Accessible button dropdownCreating an Accessible button dropdown
Creating an Accessible button dropdown
 
Cmsc 100 (web forms)
Cmsc 100 (web forms)Cmsc 100 (web forms)
Cmsc 100 (web forms)
 
Html5, css3 y js
Html5, css3 y jsHtml5, css3 y js
Html5, css3 y js
 
Mobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptMobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScript
 
p032-26
p032-26p032-26
p032-26
 
JavaScript Presentation Frameworks and Libraries
JavaScript Presentation Frameworks and LibrariesJavaScript Presentation Frameworks and Libraries
JavaScript Presentation Frameworks and Libraries
 
04. session 04 working withformsandframes
04. session 04   working withformsandframes04. session 04   working withformsandframes
04. session 04 working withformsandframes
 

Andere mochten auch

Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39kimthoa3124
 
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39kimthoa3124
 
Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653
Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653
Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653kimthoa3124
 
Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653
Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653
Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653kimthoa3124
 
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39kimthoa3124
 
Ft curriculum briefing for p3 parents
Ft curriculum briefing for p3 parentsFt curriculum briefing for p3 parents
Ft curriculum briefing for p3 parentsClara Ang
 
Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653
Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653
Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653kimthoa3124
 
Mcpppt
McppptMcpppt
Mcppptjlweb
 
23 tips to help with your video marketing strategy
23 tips to help with your video marketing strategy23 tips to help with your video marketing strategy
23 tips to help with your video marketing strategyVivek Mishra
 
Final project- Nicole Mitchell
Final project- Nicole MitchellFinal project- Nicole Mitchell
Final project- Nicole Mitchelljburleigh0722
 
APRA presentation for inDegree 2012
APRA presentation for  inDegree 2012APRA presentation for  inDegree 2012
APRA presentation for inDegree 2012Molly Wasko
 
Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653
Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653
Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653kimthoa3124
 
Blowing Performance Spa parts
Blowing Performance Spa partsBlowing Performance Spa parts
Blowing Performance Spa partsLargo, Florida
 
Nmdl presentation
Nmdl presentationNmdl presentation
Nmdl presentationTiara Ealey
 

Andere mochten auch (17)

Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
 
Communication
CommunicationCommunication
Communication
 
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
 
Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653
Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653
Căn hộ phố đông 653 trcăn mở bán đợt cuối bàn giao nhà 0989707653
 
Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653
Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653
Căn hộ phố đông quận 9 giá rẻ 11.3tr , lh 0989.707.653
 
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
Căn hộ phố đông 66 m2, 735tr, lh 0915.45.75.39
 
Ft curriculum briefing for p3 parents
Ft curriculum briefing for p3 parentsFt curriculum briefing for p3 parents
Ft curriculum briefing for p3 parents
 
Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653
Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653
Sunview 3 trung tâm gò vấp chỉ 614tr căn lh 0989.707.653
 
Mcpppt
McppptMcpppt
Mcpppt
 
23 tips to help with your video marketing strategy
23 tips to help with your video marketing strategy23 tips to help with your video marketing strategy
23 tips to help with your video marketing strategy
 
7SAMAN#55
7SAMAN#557SAMAN#55
7SAMAN#55
 
Final project- Nicole Mitchell
Final project- Nicole MitchellFinal project- Nicole Mitchell
Final project- Nicole Mitchell
 
APRA presentation for inDegree 2012
APRA presentation for  inDegree 2012APRA presentation for  inDegree 2012
APRA presentation for inDegree 2012
 
Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653
Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653
Căn hộ sunview 3 trung tâm gò vấp chỉ 614 tr căn lh 0989.707.653
 
Blowing Performance Spa parts
Blowing Performance Spa partsBlowing Performance Spa parts
Blowing Performance Spa parts
 
Nevada county gardening
Nevada county gardeningNevada county gardening
Nevada county gardening
 
Nmdl presentation
Nmdl presentationNmdl presentation
Nmdl presentation
 

Ähnlich wie WPF - Controls &amp; Data

Introduction to XAML and its features
Introduction to XAML and its featuresIntroduction to XAML and its features
Introduction to XAML and its featuresAbhishek Sur
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVMAbhishek Sur
 
Oracle11g form-course-curriculum
Oracle11g form-course-curriculumOracle11g form-course-curriculum
Oracle11g form-course-curriculumAmit Sharma
 
Oracle11g form course-curriculum
Oracle11g form course-curriculumOracle11g form course-curriculum
Oracle11g form course-curriculumAmit Sharma
 
Will your code blend? : Toronto Code Camp 2010 : Barry Gervin
Will your code blend? : Toronto Code Camp 2010 : Barry GervinWill your code blend? : Toronto Code Camp 2010 : Barry Gervin
Will your code blend? : Toronto Code Camp 2010 : Barry GervinBarry Gervin
 
Oracle fusion middleware 11g build applications with oracle forms
Oracle fusion middleware 11g build applications with oracle formsOracle fusion middleware 11g build applications with oracle forms
Oracle fusion middleware 11g build applications with oracle formsbispsolutions
 
Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databindingBoulos Dib
 
Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development Mahmoud Hamed Mahmoud
 
Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008Jonas Follesø
 
Flex3 Deep Dive Final
Flex3 Deep Dive FinalFlex3 Deep Dive Final
Flex3 Deep Dive FinalRJ Owen
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015Pushkar Chivate
 
Bringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJSBringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJSBoyan Mihaylov
 
Diving in the Flex Data Binding Waters
Diving in the Flex Data Binding WatersDiving in the Flex Data Binding Waters
Diving in the Flex Data Binding Watersmichael.labriola
 
Il 09 T3 William Spreitzer
Il 09 T3 William SpreitzerIl 09 T3 William Spreitzer
Il 09 T3 William Spreitzerwspreitzer
 
Dependency property
Dependency propertyDependency property
Dependency propertymsarangam
 

Ähnlich wie WPF - Controls &amp; Data (20)

Introduction to XAML and its features
Introduction to XAML and its featuresIntroduction to XAML and its features
Introduction to XAML and its features
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVM
 
Oracle11g form-course-curriculum
Oracle11g form-course-curriculumOracle11g form-course-curriculum
Oracle11g form-course-curriculum
 
Oracle11g form course-curriculum
Oracle11g form course-curriculumOracle11g form course-curriculum
Oracle11g form course-curriculum
 
Will your code blend? : Toronto Code Camp 2010 : Barry Gervin
Will your code blend? : Toronto Code Camp 2010 : Barry GervinWill your code blend? : Toronto Code Camp 2010 : Barry Gervin
Will your code blend? : Toronto Code Camp 2010 : Barry Gervin
 
Oracle fusion middleware 11g build applications with oracle forms
Oracle fusion middleware 11g build applications with oracle formsOracle fusion middleware 11g build applications with oracle forms
Oracle fusion middleware 11g build applications with oracle forms
 
Lesson 05 Data Binding in WPF
Lesson 05 Data Binding in WPFLesson 05 Data Binding in WPF
Lesson 05 Data Binding in WPF
 
Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databinding
 
Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development
 
WPF Concepts
WPF ConceptsWPF Concepts
WPF Concepts
 
Dev308
Dev308Dev308
Dev308
 
Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008
 
D17251 gc20 47_us
D17251 gc20 47_usD17251 gc20 47_us
D17251 gc20 47_us
 
Flex3 Deep Dive Final
Flex3 Deep Dive FinalFlex3 Deep Dive Final
Flex3 Deep Dive Final
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 
Bringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJSBringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJS
 
Diving in the Flex Data Binding Waters
Diving in the Flex Data Binding WatersDiving in the Flex Data Binding Waters
Diving in the Flex Data Binding Waters
 
Il 09 T3 William Spreitzer
Il 09 T3 William SpreitzerIl 09 T3 William Spreitzer
Il 09 T3 William Spreitzer
 
Dependency property
Dependency propertyDependency property
Dependency property
 
WPF and Databases
WPF and DatabasesWPF and Databases
WPF and Databases
 

WPF - Controls &amp; Data

  • 1. Presented by G. Sharada
  • 2. Who is this training for?  Preferably, has attended Session #1 training for WPF
  • 3. What I intend to do.  Problem solution approach: use sample applications to showcase the practical application of the topics to be covered.  Less focus on theory, more on coding.
  • 4. Word of caution  In Windows Forms, there are two ways to do everything: a good way and a bad way.  In WPF, there are ten ways to do everything: two that are amazing, 3 that are good, 1 that is bad, and 4 that suck.  It’s distinguishing the 2 amazing solutions which is so challenging. So how do we get there?
  • 5. Excellent Resources on WPF  Online Resources  MSDN  http://www.wpftutorial.net/  http://drwpf.com/blog/  http://joshsmithonwpf.wordpress.com/a-guided-tour-of-wpf/  Books  WPF 4 Unleashed  Pro WPF in C# 2010  WPF Control Development Unleashed  WPF Recipes in C# 2008  Case studies  Vertigo Family.Show  PhotoSuru
  • 6. Agenda Dependency Properties • 25 minutes Routed Events • 20 minutes Control Authoring • 25 minutes Data Binding • 25 minutes Convertors • 20 minutes Validation • 20 minutes Exercise #1 – Creating a generic form control Q&A
  • 7.
  • 8. Dependency properties  Not normal properties  A completely new implementation of properties  Needed to use core WPF features such as animation, data binding and styles.  Although, they are designed to be consumed in the same way as normal properties – but they vastly differ in their implementation behind the scenes.  Performance was the motivating factor
  • 9. Defining a dependency property  Dependency property can be added to only dependency objects  Is defined as a static field in the associated class public static readonly DependencyProperty IsSpinningProperty;
  • 10. Registering a dependency property  To use dependency property we need to register it with WPF  Is performed in a static constructor for the associated class IsSpinningProperty = DependencyProperty.Register( "IsSpinning", typeof(Boolean),
  • 11. Parameters to Register()  The property name  The data type used by the property  The type that owns this property  Optionally, a FrameworkPropertyMetadata object with additional property settings  Optionally, a callback that performs validation for the property
  • 12. Adding a Property(CLR) Wrapper  Wrap dependency property in a traditional .NET property. public bool IsSpinning { get { return (bool)GetValue(IsSpinningProperty); } set { SetValue(IsSpinningProperty, value); } }
  • 13. Property functionality  Resources  Data Binding  Styles  Animation  Property Value inheritance
  • 14. Create and use a dependency property
  • 15. Attached dependency properties  Allows different child elements to specify unique values for a property that is actually defined in a parent element.  Example: DockPanel.Dock property <DockPanel> <CheckBox DockPanel.Dock="Top"> Hello </CheckBox> </DockPanel>
  • 16. Custom Attached properties public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached( "IsBubbleSource", typeof(Boolean), typeof(AquariumObject), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender) ); public static void SetIsBubbleSource(UIElement element, Boolean value) { element.SetValue(IsBubbleSourceProperty, value); } public static Boolean GetIsBubbleSource(UIElement element) { return (Boolean)element.GetValue(IsBubbleSourceProperty); }
  • 17. Create and use a attached property
  • 18.
  • 19. Routed Events  Events with more travelling power  they can tunnel down or bubble up the element tree and be processed by event handlers along the way  Types  Direct events  Bubbling events  Tunneling events
  • 21. Registering a Routed Event public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent( "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple)); // Provide CLR accessors for the event public event RoutedEventHandler Tap { add { AddHandler(TapEvent, value); } remove { RemoveHandler(TapEvent, value); } }
  • 22. Attached events  Enables elements to handle events that are declared in a different element <Grid Button.Click="myButton_Click"> <Button Name="myButton" >Click Me</Button> </Grid>
  • 23. Add Events to a User Control
  • 24.
  • 25. Reusing existing controls  Support for rich content  Styles to alter appearance and behavior  Data templates to customize how data is displayed  Control templates to define control’s structure and appearance  Triggers to dynamically change appearance and behavior
  • 26. Creating a new control  Deriving from a user control  You want to build your control similarly to how you build an application.  Your control consists only of existing components.  You don't need to support complex customization.  Deriving from Control  You want the appearance of your control to be customizable via the ControlTemplate.  You want your control to support different themes.  Deriving from FrameworkElement (Out of scope)
  • 27. Create a Custom ToolTip Style
  • 28. Set the Content Property of a User Control
  • 29. Create a Lookless Custom Control
  • 30.
  • 31. Data Binding  The process that establishes a connection between the application UI and business logic  Use any object as your binding source and bind it to almost any target UI element  Once a binding is established, the data in the source is automatically and dynamically propagated to the binding target, and vice versa.
  • 32. Data Binding Basics  Direction of data flow
  • 33. Data Binding Basics (contd.,)  What triggers source updates
  • 34. Creating a Binding  Specifying the Binding Source  Specifying the Path to the Value <DockPanel.Resources> <c:MyData x:Key="myDataSource"/> </DockPanel.Resources> <Button Width="150" Height="30" Background="{Binding Source={StaticResource myDataSource}, Path=ColorName}">I am bound to be RED!</Button>
  • 35. Create a two-way binding
  • 36. Binding to collections  A binding source object can be treated either as a single object of which the properties contain data or as a data collection of polymorphic objects that are often grouped together
  • 37. Data templating  How the data object is displayed visually is controlled primarily by data templates, data converters, and data triggers
  • 38. Use Data Templates to Display Bound Data
  • 39.
  • 40. Data Conversion  Convert the source value of a binding in order to assign it to the target value  For example: Convert string value to type Brush
  • 41. MultiBinding Convertor  a target property has a collection of bindings  a custom IMultiValueConverter is used to produce a final value from the values of the bindings <TextBlock Name="textBox2" DataContext="{StaticResource NameListData}"> <TextBlock.Text> <MultiBinding Converter="{StaticResource myNameConverter}" ConverterParameter="FormatLastFirst"> <Binding Path="FirstName"/> <Binding Path="LastName"/> </MultiBinding> </TextBlock.Text> </TextBlock>
  • 42. Use Value Converters to Convert Bound Data
  • 43.
  • 44. Data Validation  Associating Validation Rules with a Binding  ExceptionValidationRule - checks for exceptions thrown during the update of the binding source property  DataErrorValidationRule - object checks for errors that are raised by objects that implement the IDataErrorInfo interface  Providing Visual Feedback  Use Validation.HasError property  Use Validation.ErrorTemplate property
  • 45. Specify Validation Rules for a Binding
  • 47. Exercise: Create a generic form control  Dependency properties for form-title, help-text  Dependency property for IsValid  Routed events for Submit and Cancel  Attached property to specify Submit and Cancel buttons  Attached property to specify input control(s) to be validated  Data template for View mode  Control template for Edit mode