Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Wpf from David Kossoglyad


Published on

This is sample presentation that I can provide to my potential clients.

Published in: Business, Real Estate
  • Be the first to comment

  • Be the first to like this

Wpf from David Kossoglyad

  1. 1. From David Kossoglyad
  2. 2.
  3. 3. Dependency Properties <ul><li>Dependency property: A property that is backed by an instance of the DependencyProperty class. </li></ul><ul><li>Dependency property identifier: A DependencyProperty instance, which is obtained as a return value when registering a dependency property, and then stored as a member of a class. </li></ul><ul><li>CLR &quot;wrapper&quot;: The actual get and set implementations for the property. </li></ul>
  4. 4. Dependency Properties <ul><li>You need to add a property to a DependencyObject that provides support for any or all of the following: </li></ul><ul><ul><li>Data bindings </li></ul></ul><ul><ul><li>Animation </li></ul></ul><ul><ul><li>Setting with a dynamic resource reference </li></ul></ul><ul><ul><li>Automatically inheriting a property value from a super-class </li></ul></ul><ul><ul><li>Setting in a style </li></ul></ul><ul><ul><li>Using property value inheritance (see recipe 1-9) </li></ul></ul><ul><ul><li>Notification through a callback when the value changes </li></ul></ul>
  5. 5. Dependency Property Definition <ul><li>Possible to define only on class inherits DependencyObject class. </li></ul><ul><li>Step 1 – Define dependency property identifier. </li></ul><ul><li>public static readonly DependencyProperty NameProperty = </li></ul><ul><li>DependencyProperty. Register( </li></ul><ul><li>&quot;Name&quot;, </li></ul><ul><li>typeof(string), </li></ul><ul><li>typeof( Person), </li></ul><ul><li>new FrameworkPropertyMetadata( </li></ul><ul><li>0, FrameworkPropertyMetadataOptions. BindsTwoWayByDefault, </li></ul><ul><li>new PropertyChangedCallback( NameProperty_Changed), </li></ul><ul><li>new CoerceValueCallback( NameProperty_CoerceValue), </li></ul><ul><li>false, </li></ul><ul><li>UpdateSourceTrigger. PropertyChanged), </li></ul><ul><li>new ValidateValueCallback( Name_Validate) ); </li></ul>
  6. 6. Dependency Properties <ul><li>Who is who in Property Metadata? </li></ul><ul><ul><li>The base class PropertyMetadata isn’t abstract and usable for a minimal or default metadata definition. </li></ul></ul><ul><ul><li>Two classes, UIPropertyMetadata and FrameworkPropertyMetadata, are advanced versions of PropertyMetadata. </li></ul></ul><ul><ul><li>FrameworkMetadataOptions. May be significant when you define dependency property for a control. </li></ul></ul><ul><ul><li>PropertyChangedCallback. </li></ul></ul>
  7. 7. Dependency Properties <ul><li>Who is who in Property Metadata? </li></ul><ul><ul><li>CoerceValueCallback. </li></ul></ul><ul><ul><li>UpdateSourceTrigger </li></ul></ul><ul><ul><li>Possible override metadata without reregister dependency property or registering new owner of that property: </li></ul></ul><ul><ul><li>DataContextProperty.OverrideMetadata( </li></ul></ul><ul><ul><li>typeof(Window1), </li></ul></ul><ul><ul><li>new FrameworkPropertyMetadata( </li></ul></ul><ul><ul><li>100d, </li></ul></ul><ul><ul><li>new PropertyChangedCallback(DataContext_PropertyChanged))); </li></ul></ul>
  8. 8. Dependency Properties <ul><li>Perfomance tip: </li></ul><ul><ul><li>Always, when you define dependency property with the same name and type like as a property defined once, use AddOwner instead of Register. </li></ul></ul><ul><li>Architecture tip: </li></ul><ul><ul><li>Define read-only dependency property with RegisterReadOnly method. </li></ul></ul>
  9. 9. Dependency Properties <ul><li>Step 2 – CLR Property Definition. </li></ul><ul><li>public string Name </li></ul><ul><li>{ </li></ul><ul><li> get </li></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>return (string)GetValue(NameProperty); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li> set </li></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>SetValue(NameProperty, value); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  10. 10. Dependency Properties <ul><li>Step 3 (optional) – </li></ul><ul><ul><li>Implement PropertyChangedCallback handler. </li></ul></ul><ul><ul><li>Implement CoerceValueCallback handler. </li></ul></ul>
  11. 11. Dependency Property <ul><li>Attached Dependency Properties </li></ul><ul><ul><li>You can think of an attached property as a special type of dependency property that doesn’t get exposed using a CLR property wrapper. </li></ul></ul><ul><ul><li>Use it when you need to add a dependency property to a class but are not able to access the class in a way that would allow you to add the property, or you want to use a property that can be set on any child objects of the type. </li></ul></ul>
  12. 12.
  13. 13. Routed Events <ul><li>Definitions from official Microsoft Documentation. </li></ul><ul><li>Functional definition: A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event. </li></ul><ul><li>Implementation definition: A routed event is a CLR event that is backed by an instance of the RoutedEvent class and is processed by the Windows Presentation Foundation (WPF) event system. </li></ul>
  14. 14. Routed Events <ul><li>Sample Code: </li></ul><ul><li>public static readonly RoutedEvent TapEvent = </li></ul><ul><li>EventManager. RegisterRoutedEvent( </li></ul><ul><li>&quot;Tap&quot;, </li></ul><ul><li>RoutingStrategy. Bubble, </li></ul><ul><li>typeof( RoutedEventHandler), </li></ul><ul><li>typeof( MyButtonSimple)); </li></ul><ul><li>// Provide CLR accessors for the event </li></ul><ul><li>public event RoutedEventHandler Tap </li></ul><ul><li>{ </li></ul><ul><li>add { AddHandler(TapEvent, value); } </li></ul><ul><li>remove { RemoveHandler(TapEvent, value); } </li></ul><ul><li>} </li></ul>
  15. 15. Routed Events <ul><li>Routing Strategies </li></ul><ul><ul><li>Bubbling. </li></ul></ul><ul><ul><li>Direct. </li></ul></ul><ul><ul><li>Tunneling. </li></ul></ul>
  16. 16.
  17. 17. Routed Commands <ul><li>A Command represents an operation, that may be reused in scope of the application. </li></ul><ul><li>Only the commands, which are of type RoutedCommand (or derived type) are true Routed Commands. </li></ul>
  18. 18.
  19. 19. StaticResource, DynamicResource, etc. <ul><li>A markup extension can be implemented to provide values for properties in attribute syntax, properties in property element syntax, or both. </li></ul><ul><li>StaticResource - Provides a value for a XAML property by substituting the value of an already defined resource </li></ul><ul><li>DynamicResource  - Provides a value for a XAML property by deferring that value to be a runtime reference to a resource. A dynamic resource reference forces a new lookup each time that such a resource is accessed. </li></ul>
  20. 20. StaticResource, DynamicResource, etc. <ul><li>Perfomance tip: </li></ul><ul><ul><li>Use StaticResource instead of DynamicResource whenever is possible. </li></ul></ul>