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 topi...
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 ...
Excellent Resources on WPF Online Resources    MSDN    http://www.wpftutorial.net/    http://drwpf.com/blog/    http:...
Agenda     Dependency Properties             • 25 minutes         Routed Events                 • 20 minutes       Control...
Dependency properties Not normal properties A completely new implementation of properties Needed to use core WPF featur...
Defining a dependency property Dependency property can be added to only  dependency objects Is defined as a static field...
Registering a dependency property To use dependency property we need to register it with  WPF Is performed in a static c...
Parameters to Register() The property name The data type used by the property The type that owns this property Optiona...
Adding a Property(CLR) Wrapper Wrap dependency property in a traditional .NET    property.public bool IsSpinning {    get...
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 ...
Custom Attached propertiespublic static readonly DependencyProperty  IsBubbleSourceProperty =  DependencyProperty.Register...
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 process...
Tunneling and Bubbling events
Registering a Routed Eventpublic static readonly RoutedEvent TapEvent =  EventManager.RegisterRoutedEvent( "Tap",  Routing...
Attached events Enables elements to handle events that are declared in a different element<Grid Button.Click="myButton_Cl...
Add Events to a User Control
Reusing existing controls Support for rich content Styles to alter appearance and behavior Data templates to customize ...
Creating a new control Deriving from a user control   You want to build your control similarly to how you    build an ap...
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 ...
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...
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 d...
Data templating How the data object is displayed visually is controlled  primarily by data templates, data converters, an...
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 st...
MultiBinding Convertor a target property has a collection of bindings a custom IMultiValueConverter is used to produce a...
Use Value Converters to Convert Bound Data
Data Validation Associating Validation Rules with a Binding    ExceptionValidationRule - checks for exceptions thrown   ...
Specify Validation Rules for a Binding
Bind to IDataErrorInfo
Exercise: Create a generic formcontrol Dependency properties for form-title, help-text Dependency property for IsValid ...
WPF - Controls &amp; Data
WPF - Controls &amp; Data
WPF - Controls &amp; Data
WPF - Controls &amp; Data
WPF - Controls &amp; Data
WPF - Controls &amp; Data
WPF - Controls &amp; Data
Upcoming SlideShare
Loading in …5
×

WPF - Controls &amp; Data

515 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
515
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

WPF - Controls &amp; Data

  1. 1. Presented by G. Sharada
  2. 2. Who is this training for? Preferably, has attended Session #1 training for WPF
  3. 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. 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. 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. 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. 7. 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
  8. 8. Defining a dependency property Dependency property can be added to only dependency objects Is defined as a static field in the associated classpublic static readonly DependencyProperty IsSpinningProperty;
  9. 9. Registering a dependency property To use dependency property we need to register it with WPF Is performed in a static constructor for the associated classIsSpinningProperty = DependencyProperty.Register( "IsSpinning", typeof(Boolean),
  10. 10. 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
  11. 11. 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); }}
  12. 12. Property functionality Resources Data Binding Styles Animation Property Value inheritance
  13. 13. Create and use a dependency property
  14. 14. 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>
  15. 15. Custom Attached propertiespublic 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); }
  16. 16. Create and use a attached property
  17. 17. 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
  18. 18. Tunneling and Bubbling events
  19. 19. Registering a Routed Eventpublic static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent( "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));// Provide CLR accessors for the eventpublic event RoutedEventHandler Tap { add { AddHandler(TapEvent, value); } remove { RemoveHandler(TapEvent, value); }}
  20. 20. 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>
  21. 21. Add Events to a User Control
  22. 22. 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
  23. 23. 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 dont 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)
  24. 24. Create a Custom ToolTip Style
  25. 25. Set the Content Property of a User Control
  26. 26. Create a Lookless Custom Control
  27. 27. 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.
  28. 28. Data Binding Basics Direction of data flow
  29. 29. Data Binding Basics (contd.,) What triggers source updates
  30. 30. 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>
  31. 31. Create a two-way binding
  32. 32. 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
  33. 33. Data templating How the data object is displayed visually is controlled primarily by data templates, data converters, and data triggers
  34. 34. Use Data Templates to Display Bound Data
  35. 35. 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
  36. 36. 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>
  37. 37. Use Value Converters to Convert Bound Data
  38. 38. 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
  39. 39. Specify Validation Rules for a Binding
  40. 40. Bind to IDataErrorInfo
  41. 41. Exercise: Create a generic formcontrol 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

×