WPF Concepts


Published on

Published in: Business, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

WPF Concepts

  1. 1. XAML Concepts Dependency Properties, XAML Trees, Routing <ul><li>Doncho Minkov </li></ul><ul><li>Telerik School Academy </li></ul><ul><li>schoolacademy.telerik.com </li></ul><ul><li>Technical Trainer </li></ul><ul><li>http://www.minkov.it </li></ul>
  2. 2. Table of Contents <ul><li>Dependency Objects </li></ul><ul><li>Dependency Properties </li></ul><ul><li>Attached Properties </li></ul><ul><li>Trees in XAML </li></ul><ul><ul><li>Trees in WPF </li></ul></ul><ul><ul><li>Trees in Silverlight </li></ul></ul><ul><ul><li>VisualTreeHelper </li></ul></ul><ul><ul><li>LogicalTreeHelper </li></ul></ul>
  3. 3. Table of Contents (2) <ul><li>Routing </li></ul><ul><ul><li>Bubbling </li></ul></ul><ul><ul><li>Tunneling </li></ul></ul><ul><li>Commanding in XAML </li></ul><ul><ul><li>Built-in commands </li></ul></ul><ul><ul><li>ICommand </li></ul></ul><ul><ul><li>The Relay Command </li></ul></ul>
  4. 4. Dependency Object
  5. 5. Dependency Object <ul><li>The DependencyObject </li></ul><ul><ul><li>Represents an object that participates in the dependency property system </li></ul></ul><ul><ul><li>Enables WPF / SL property system services </li></ul></ul><ul><li>The property system's functions: </li></ul><ul><ul><li>Compute the values of properties </li></ul></ul><ul><ul><li>Provide system notification about values that have changed </li></ul></ul><ul><li>DependencyObject as a base class enables objects to use Dependency Properties </li></ul>
  6. 6. Dependency Object (2) <ul><li>DependencyObject has the following </li></ul><ul><ul><li>Get , Set , and Clear methods for values of any dependency properties </li></ul></ul><ul><ul><li>Metadata, coerce value support, property changed notification </li></ul></ul><ul><ul><li>Override callbacks for dependency properties or attached properties </li></ul></ul><ul><li>DependencyObject class facilitates the per-owner property metadata for a dependency property </li></ul>
  7. 7. Dependency Properties Dependencies
  8. 8. Dependency Properties <ul><li>Silverlight and WPF provide a set of services that can be used to extend the functionality of a CLR property </li></ul><ul><ul><li>Collectively, these services are typically referred to as the Silverlight / WPF property system </li></ul></ul><ul><li>Dependency Property is </li></ul><ul><ul><li>A property that is backed by the SL/WPF property system </li></ul></ul>
  9. 9. Dependency Properties (2) <ul><li>Dependency properties are typically exposed as CLR properties </li></ul><ul><ul><li>At a basic level, you could interact with these properties directly </li></ul></ul><ul><ul><li>May never find out they are dependency properties </li></ul></ul><ul><li>Better to know if a property is Dependency or CLR </li></ul><ul><ul><li>Can use the advantages of the dependency properties </li></ul></ul>
  10. 10. Dependency Properties (3) <ul><li>The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs </li></ul><ul><ul><li>Can be implemented to provide callbacks to propagate changes to other properties </li></ul></ul>
  11. 11. Dependency Properties Live Demo
  12. 12. Attached Properties How to set properties from another place
  13. 13. Attached Properties <ul><li>An attached property is intended to be used as a type of global property that is settable on any object </li></ul><ul><li>In WPF and Silverlight attached properties are defined as dependency properties </li></ul><ul><ul><li>They don't have the wrapper property </li></ul></ul><ul><li>Examples of Attached Properties </li></ul><ul><ul><li>Grid.Row, Grid.Column, Grid.RowSpan </li></ul></ul><ul><ul><li>Canvas.Top, Canvas.Left, Canvas.Bottom </li></ul></ul><ul><ul><li>etc. </li></ul></ul>
  14. 14. Attached Properties Live Demo
  15. 15. Custom Dependency Properties How to make our own Dependency Properties?
  16. 16. Custom Dependency Properties <ul><li>The first thing to do is to register the Dependency Property </li></ul><ul><ul><li>Need registration due to the Property System </li></ul></ul><ul><ul><li>In most of the cases we need a dependency property on a UserControl </li></ul></ul><ul><li>public static readonly </li></ul><ul><li>DependencyProperty ScrollValueProperty = </li></ul><ul><li>DependencyProperty.Register( </li></ul><ul><li>&quot;ScrollValue&quot;, </li></ul><ul><li>typeof(double), </li></ul><ul><li>typeof(UserControl), </li></ul><ul><li>null); </li></ul>Dependency Property's instance is always readonly The name of the Dependency Property Registration to the Property System
  17. 17. Dependency Property Registration <ul><li>Two Register Methods: </li></ul><ul><ul><li>Register(String, Type, Type) </li></ul></ul><ul><ul><ul><li>Registers a dependency property with the specified property name, property type, and owner type </li></ul></ul></ul><ul><ul><li>Register(String, Type, Type, PropertyMetadata) </li></ul></ul><ul><ul><ul><li>Add Property metadata </li></ul></ul></ul><ul><ul><ul><li>Default value or Callback for Property changes </li></ul></ul></ul>
  18. 18. Dependency Property Wrapper <ul><li>After the registration of the Dependency Property it needs wrapper </li></ul><ul><ul><li>Used to make it look like plain old CLR Property </li></ul></ul><ul><li>DependencyObject has two methods used for the wrapping of dependency properties </li></ul><ul><ul><li>SetValue(DependenyProperty, value) </li></ul></ul><ul><ul><li>GetValue(DependenyProperty) </li></ul></ul><ul><li>public double ScrollValue </li></ul><ul><li>{ </li></ul><ul><li>get { return (double)GetValue(ScrollValueProperty); } </li></ul><ul><li>set { SetValue(ScrollValueProperty , value); } </li></ul><ul><li>} </li></ul>
  19. 19. Custom Attached Properties How to make attached properties?
  20. 20. Custom Attached Properties <ul><li>The registration of attached properties is a little different </li></ul><ul><li>private static void OnPropertyChanged(…) { … } </li></ul><ul><li>public static Thickness GetMargin(DependencyObject obj) </li></ul><ul><li>{ </li></ul><ul><li>return (Thickness)obj.GetValue(MarginProperty); </li></ul><ul><li>} </li></ul><ul><li>public static void SetMargin(DependencyObject obj, Thickness val) </li></ul><ul><li>{ </li></ul><ul><li>obj.SetValue(MarginProperty, val); </li></ul><ul><li>} </li></ul><ul><li>public static readonly DependencyProperty MarginProperty = </li></ul><ul><li>DependencyProperty.RegisterAttached(&quot;Margin&quot;, </li></ul><ul><li>typeof(Thickness), typeof(ContentMargin), </li></ul><ul><li>new FrameworkPropertyMetadata(default(Thickness), </li></ul><ul><li>new PropertyChangedCallback(OnPropertyChanged))); </li></ul>
  21. 21. Custom Dependency and Attached Properties Live Demo
  22. 22. Trees in WPF Visual and Logical
  23. 23. Trees in WPF and Silverlight <ul><li>WPF and Silverlight use a hierarchical system that organizes the elements and components </li></ul><ul><ul><li>Developers can manipulate the nodes directly </li></ul></ul><ul><ul><ul><li>Affect the rendering or behavior of an application </li></ul></ul></ul><ul><li>Two such trees exist in WPF </li></ul><ul><ul><li>Logical tree and Visual tree </li></ul></ul><ul><li>One kind of tree in Silverlight </li></ul><ul><ul><li>Visual Tree </li></ul></ul>
  24. 24. Trees in WPF and Silverlight <ul><li>Elements of a XAML are hierarchically related </li></ul><ul><ul><li>This relation is called the LogicalTree </li></ul></ul><ul><li>The template of one element consists of multiple visual elements </li></ul><ul><ul><li>This tree is called the VisualTree </li></ul></ul><ul><li>WPF differs between those two trees </li></ul><ul><ul><li>Some problems are solved only by the logical elements </li></ul></ul><ul><ul><li>For others you want all elements </li></ul></ul>
  25. 25. Trees in WPF Visual and Logical
  26. 26. The Trees in WPF <ul><li>WPF supports two kinds of Trees for rendering </li></ul><ul><ul><li>Logical Tree </li></ul></ul><ul><ul><ul><li>Describes the structure of control elements </li></ul></ul></ul><ul><ul><li>Visual Tree </li></ul></ul><ul><ul><ul><li>Describes the structure of Visual elements </li></ul></ul></ul><ul><ul><li>Sometimes both trees are used the same way </li></ul></ul>
  27. 27. Object Tree <ul><li>The Object Tree </li></ul>Window Border AdornedDecoration AdornedLayer ContentPresenter StackPanel Label Border ContentPresenter TextBlock Button Border ContentPresenter TextBlock
  28. 28. Logical Tree <ul><li>The Logical Tree </li></ul>Window Border AdornedDecoration AdornedLayer ContentPresenter StackPanel Label Border ContentPresenter TextBlock Button Border ContentPresenter TextBlock
  29. 29. Visual Tree <ul><li>The Visual Tree </li></ul>Window Border AdornedDecoration AdornedLayer ContentPresenter StackPanel Label Border ContentPresenter TextBlock Button Border ContentPresenter TextBlock
  30. 30. Why Two Kinds of Trees? <ul><li>A WPF control consists of multiple, more primitive controls </li></ul><ul><ul><li>A button consists of </li></ul></ul><ul><ul><ul><li>A border, a rectangle and a content presenter. </li></ul></ul></ul><ul><ul><ul><li>These controls are visual children of the button </li></ul></ul></ul><ul><li>When WPF renders the button </li></ul><ul><ul><li>The element itself has no appearance </li></ul></ul><ul><ul><li>It iterates through the visual tree and renders the visual children of it </li></ul></ul>
  31. 31. Why Two Kinds of Trees? (2) <ul><li>Sometimes you are not interested in the borders and rectangles of a controls' template </li></ul><ul><ul><li>You want a more robust tree that only contains the &quot;real&quot; controls </li></ul></ul><ul><ul><ul><li>Not all the template parts </li></ul></ul></ul><ul><ul><ul><li>And that is the eligibility for the logical tree </li></ul></ul></ul>
  32. 32. The Logical Tree <ul><li>The logical tree describes the relations between elements of the user interface </li></ul><ul><li>The logical tree is responsible for: </li></ul><ul><ul><li>Inherit DependencyProperty values </li></ul></ul><ul><ul><li>Resolving DynamicResources references </li></ul></ul><ul><ul><li>Looking up element names for bindings </li></ul></ul><ul><ul><li>Forwarding RoutedEvents </li></ul></ul>
  33. 33. The Visual Tree <ul><li>Contains all logical elements </li></ul><ul><ul><li>Including all visual elements of the template of each element </li></ul></ul><ul><li>The visual tree is responsible for: </li></ul><ul><ul><li>Rendering visual elements </li></ul></ul><ul><ul><li>Propagate element opacity </li></ul></ul><ul><ul><li>Propagate Layout- and RenderTransforms </li></ul></ul><ul><ul><li>Propagate the IsEnabled property </li></ul></ul><ul><ul><li>Do Hit-Testing </li></ul></ul><ul><ul><li>RelativeSource (FindAncestor) </li></ul></ul>
  34. 34. Traversing Through Trees in WPF VisualTreeHelper and Logical Tree Helper
  35. 35. LogicalTreeHelper and VisualTreeHelper <ul><li>Help a lot when traversing the WPF Trees </li></ul><ul><li>Key Functionality: </li></ul><ul><ul><li>GetParrent(Dependency Object) </li></ul></ul><ul><ul><ul><li>Gets the logical parent of the current element </li></ul></ul></ul><ul><ul><li>GetChildren(Dependency Object) </li></ul></ul><ul><ul><li>GetOpacity(Dependency Object) </li></ul></ul><ul><ul><ul><li>Etc… </li></ul></ul></ul>
  36. 36. Traversing Through Trees in WPF Live Demo
  37. 37. Visual Tree in Silverlight Just the Visual
  38. 38. The Visual Tree in Silverlight <ul><li>The same as in WPF </li></ul><ul><ul><li>Works exactly as in WPF </li></ul></ul><ul><ul><li>May be used to find the first ancestor of concrete type </li></ul></ul><ul><ul><ul><li>i.e. the first Grid or StackPanel </li></ul></ul></ul><ul><li>public static T FindUpVisualTree<T> </li></ul><ul><li>(DependencyObject initial) where T : DependencyObject </li></ul><ul><li>{ </li></ul><ul><li>DependencyObject current = initial; </li></ul><ul><li>while (current != null && </li></ul><ul><li>current.GetType() != typeof(T)) </li></ul><ul><li>{ current = VisualTreeHelper.GetParent(current); } </li></ul><ul><li>return current as T; </li></ul><ul><li>} </li></ul>
  39. 39. Visual Tree in Silverlight Live Demo
  40. 40. Routed Events in WPF/Silverlight Bubbling and Tunneling
  41. 41. Routed Events <ul><li>What is a routed event? </li></ul><ul><ul><li>A type of event that can invoke handlers on multiple listeners in an element tree </li></ul></ul><ul><ul><ul><li>Rather than just on the object that raised it </li></ul></ul></ul><ul><li>The event route can travel in one of two directions </li></ul><ul><ul><li>Depending on the event definition </li></ul></ul><ul><ul><li>Generally the route travels from the source element and then &quot;bubbles&quot; upward through the element tree </li></ul></ul>
  42. 42. Types of Routed Events <ul><li>Three types of routed events in WPF and SL </li></ul><ul><ul><li>Bubbling </li></ul></ul><ul><ul><ul><li>Event handlers on the event source are invoked </li></ul></ul></ul><ul><ul><ul><li>Then routes to successive parent elements until reaching the element tree root </li></ul></ul></ul><ul><ul><ul><li>Most routed events use bubbling routing strategy </li></ul></ul></ul><ul><ul><li>Direct </li></ul></ul><ul><ul><ul><li>Only the source element itself is given the opportunity to invoke handlers in response </li></ul></ul></ul>
  43. 43. Types of Routed Events (2) <ul><li>Three types of routed events in WPF and SL </li></ul><ul><ul><li>Tunneling </li></ul></ul><ul><ul><ul><li>Event handlers at the tree root are invoked first </li></ul></ul></ul><ul><ul><ul><li>Then travels down the object tree to the node that is the source of the event </li></ul></ul></ul><ul><ul><ul><ul><li>The element that raised the routed event </li></ul></ul></ul></ul><ul><ul><ul><li>Not supported in Silverlight </li></ul></ul></ul><ul><ul><li>Available as Preview events </li></ul></ul><ul><ul><ul><li>PreviewClick </li></ul></ul></ul>
  44. 44. Routed Events Example Tunneling Window Grid StackPanel TextBlock PreviewMouseLeftButtonDown Event is raised
  45. 45. Routed Events Example Window Grid StackPanel TextBlock Bubbling MouseLeftButtonDown Event is raised
  46. 46. Routed Events in WPF/Silverlight Live Demo
  47. 47. Commands in .NET
  48. 48. WPF Commands <ul><li>Commanding is an input mechanism in WPF </li></ul><ul><ul><li>Provides input handling at a more semantic level than device input </li></ul></ul><ul><ul><li>Examples of commands are the Copy , Cut , and Paste operations </li></ul></ul>
  49. 49. WPF Commands (2) <ul><li>Commands have several purposes </li></ul><ul><ul><li>Separate the semantics and the objects that invoke a command from the logic that executes the command </li></ul></ul><ul><ul><ul><li>Allows for multiple and disparate sources to invoke the same command logic </li></ul></ul></ul><ul><ul><ul><li>Allows the command logic to be customized for different targets </li></ul></ul></ul>
  50. 50. WPF Commands <ul><li>Commands can be used to indicate whether an action is available </li></ul><ul><ul><li>Example: when trying to cut something, the user should first select something </li></ul></ul><ul><ul><li>To indicate whether an action is possible </li></ul></ul><ul><ul><ul><li>Implement the CanExecute method </li></ul></ul></ul><ul><ul><li>A button can subscribe to the CanExecuteChanged event </li></ul></ul><ul><ul><ul><li>Disabled if CanExecute returns false </li></ul></ul></ul><ul><ul><ul><li>Enabled if CanExecute returns true. </li></ul></ul></ul>
  51. 51. The Four Main Concepts in WPF Commanding <ul><li>The routed command model in WPF consists of four main concepts </li></ul><ul><ul><li>Command </li></ul></ul><ul><ul><ul><li>The action to be executed </li></ul></ul></ul><ul><ul><li>CommandSource </li></ul></ul><ul><ul><ul><li>The object that invokes the command </li></ul></ul></ul><ul><ul><li>CommandTarget </li></ul></ul><ul><ul><ul><li>The object that the command is executed on </li></ul></ul></ul><ul><ul><li>CommandBinding </li></ul></ul><ul><ul><ul><li>The object that maps command logic to command </li></ul></ul></ul>
  52. 52. Four Main Concepts in WPF Commanding Example <ul><li><Menu> </li></ul><ul><li><MenuItem Command=&quot;Copy&quot; </li></ul><ul><li>CommandTarget=&quot;{Binding ElementName=textBoxText}&quot; /> </li></ul><ul><li><MenuItem Command=&quot;Paste&quot; </li></ul><ul><li>CommandTarget=&quot;{Binding ElementName=mainTextBox}&quot; /> </li></ul><ul><li></Menu> </li></ul><ul><li><TextBox Name=&quot;mainTextBox&quot;/> </li></ul><ul><li><TextBox Name=&quot;textBoxText&quot;> </li></ul><ul><li>Some Text in a Text Box </li></ul><ul><li></TextBox> </li></ul>
  53. 53. Commands in .NET Live Demo
  54. 54. The ICommand Interface How to implement our own Commands
  55. 55. ICommand Interface <ul><li>The ICommand interface </li></ul><ul><li>public bool CanExecute(object parameter); </li></ul><ul><li>public event EventHandler CanExecuteChanged; </li></ul><ul><li>public void Execute(object parameter); </li></ul>Determines whether the command can be executed When changes of the CanExecute state occur Called to invoke the command
  56. 56. Implementation Command Example <ul><li>Lets implement a Command to show the selected text in a TextBox </li></ul><ul><li>class MessagePopCommand : ICommand </li></ul><ul><li>{ </li></ul><ul><li>public bool CanExecute(object parameter) </li></ul><ul><li>{ </li></ul><ul><li>if (parameter == null) </li></ul><ul><li>{ </li></ul><ul><li>return false; </li></ul><ul><li>} </li></ul><ul><li>return !string.IsNullOrEmpty(parameter.ToString()); </li></ul><ul><li>} </li></ul><ul><li>public event EventHandler CanExecuteChanged; </li></ul><ul><li>public void Execute(object parameter) </li></ul><ul><li>{ </li></ul><ul><li>MessageBox.Show(parameter.ToString()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  57. 57. Implementing Command Example <ul><li>We need to make an instance of the Command in the code behind </li></ul><ul><li>The XAML file: </li></ul><ul><li><TextBox Name=&quot;TextBoxToShow&quot;>text</TextBox> </li></ul><ul><li><Button Content=&quot;Click Me!&quot; </li></ul><ul><li>CommandParameter=&quot;{Binding ElementName=TextBoxToShow, </li></ul><ul><li>Path=Text}&quot; </li></ul><ul><li>Command=&quot;{Binding MessageCommand}&quot;/> </li></ul><ul><li>In the Code Behind file: </li></ul>private ICommand messageCommand; public ICommand MessageCommand { get { return this.messageCommand; } }
  58. 58. How Does it Work? <ul><li>When binding the command of the button to a specific command instance, the CanExecute method is invoked </li></ul><ul><ul><li>If it returns false the button is disabled </li></ul></ul><ul><ul><li>If true is returned the button is enabled </li></ul></ul><ul><li>A known problem </li></ul><ul><ul><li>The order of the Command and CommandParameter properties matters! </li></ul></ul><ul><ul><ul><li>The XAML parser works from left to right </li></ul></ul></ul><ul><ul><ul><li>The paramerters must be known before binding </li></ul></ul></ul>
  59. 59. The ICommand Interface Live Implementation
  60. 60. Better Commanding Even better than Custom Commands
  61. 61. Better Commanding <ul><li>Most of the times it is not necessary to implement ICommand class for every distinct command </li></ul><ul><ul><li>Since in most of the cases the ConcreteCommand has the same interface </li></ul></ul><ul><li>Can we implement a command and give different behavior then instantiating? </li></ul><ul><ul><li>Of course – use the so called RelayCommand </li></ul></ul>
  62. 62. The RelayCommand <ul><li>What is a relay command </li></ul><ul><ul><li>A command which is given an behavior during instantiating </li></ul></ul><ul><ul><li>Both CanExecute and Execute methods </li></ul></ul><ul><li>ICommand someCommand; </li></ul><ul><li>public MyWindow() </li></ul><ul><li>{ </li></ul><ul><li>this.someCommand = </li></ul><ul><li>new RelayCommand(ExecuteMethod,CanExecuteMethod); } </li></ul><ul><li>public void ExecuteMethod(object parameter) {…} </li></ul><ul><li>public bool CanExecuteMethod(object parameter) {…} </li></ul>
  63. 63. Better Commanding Live Demo
  64. 64. What's the Point of Commands?! Why the hell we need Commands?
  65. 65. The Point of Commands <ul><li>The answer is simple: </li></ul><ul><ul><li>The Commands can execute without the knowledge of who wants to execute them </li></ul></ul><ul><li>Commands are: </li></ul><ul><ul><li>Easily implemented </li></ul></ul><ul><ul><li>Easily extended </li></ul></ul><ul><ul><li>Easily replaceable </li></ul></ul><ul><ul><li>A way to change an object without knowledge of who wants to change it </li></ul></ul><ul><ul><li>Fundamental part of the MVVM pattern </li></ul></ul>
  66. 66. XAML Concepts
  67. 67. Exercises <ul><li>Extend the VideoPlayer Control from the example: </li></ul><ul><ul><li>Add a slider that slides with the video length </li></ul></ul><ul><ul><li>Add a slider that changes the volume </li></ul></ul><ul><ul><li>Add buttons for Play, Pause, Stop </li></ul></ul><ul><ul><li>Add key events for volume UP/DOWN </li></ul></ul><ul><ul><li>Add key events to jump 5 seconds forward/backward in the video </li></ul></ul>