Académique Documents
Professionnel Documents
Culture Documents
Table of Contents
Dependency Objects Dependency Properties
Attached Properties
Trees in XAML
Trees in WPF
Trees in Silverlight VisualTreeHelper LogicalTreeHelper
Bubbling
Tunneling
Commanding in XAML
Built-in commands
ICommand The Relay Command
Dependency Object
Dependency Object
The DependencyObject
Represents an object that participates in the dependency property system Enables WPF / SL property system services
The property system's functions:
Compute the values of properties Provide system notification about values that have changed
DependencyObject as a base class enables
Get, Set, and Clear methods for values of any dependency properties Metadata, coerce value support, property changed notification Override callbacks for dependency properties or attached properties
DependencyObject class
Dependency Properties
Dependencies
Dependency Properties
Silverlight
and WPF provide a set of services that can be used to extend the functionality of a CLR property
Collectively, these services are typically referred to as the Silverlight / WPF property system
Dependency Property is
exposed
as CLR properties
At a basic level, you could interact with these properties directly
CLR
Can use the advantages of the dependency properties
is to provide a way to compute the value of a property based on the value of other inputs
Can be implemented to provide callbacks to propagate changes to other properties
Dependency Properties
Live Demo
Attached Properties
How to set properties from another place
Attached Properties
An attached property is intended to be used as
etc.
Attached Properties
Live Demo
DependencyObject has two methods used for the wrapping of dependency properties
SetValue(DependenyProperty, value)
GetValue(DependenyProperty)
public double ScrollValue { get { return (double)GetValue(ScrollValueProperty); } set { SetValue(ScrollValueProperty , value); } }
private static void OnPropertyChanged() { } public static Thickness GetMargin(DependencyObject obj) { return (Thickness)obj.GetValue(MarginProperty); } public static void SetMargin(DependencyObject obj, Thickness val) { obj.SetValue(MarginProperty, val); } public static readonly DependencyProperty MarginProperty = DependencyProperty.RegisterAttached("Margin", typeof(Thickness), typeof(ContentMargin), new FrameworkPropertyMetadata(default(Thickness), new PropertyChangedCallback(OnPropertyChanged)));
Trees in WPF
Visual and Logical
Visual Tree
related of
Some problems are solved only by the logical elements For others you want all elements
Trees in WPF
Visual and Logical
Logical Tree
Describes the structure of control elements
Visual Tree
Describes the structure of Visual elements
Object Tree
Window Border AdornedDecoration
StackPanel
ContentPresenter
AdornedLayer
TextBlock
Logical Tree
Window Border AdornedDecoration
StackPanel
ContentPresenter
AdornedLayer
TextBlock
Visual Tree
Window Border AdornedDecoration
StackPanel
ContentPresenter
AdornedLayer
TextBlock
of multiple, more
primitive controls
A button consists of
A border, a rectangle and a content presenter.
The element itself has no appearance It iterates through the visual tree and renders the visual children of it
for:
Forwarding RoutedEvents
Rendering visual elements Propagate element opacity Propagate Layout- and RenderTransforms
GetParrent(Dependency Object)
Gets the logical parent of the current element
GetChildren(Dependency Object)
GetOpacity(Dependency Object)
Etc
Routed Events
What is a routed event?
A type of event that can invoke handlers on multiple listeners in an element tree
Rather than just on the object that raised it
The event route can travel in one of two
directions
Depending on the event definition
Generally the route travels from the source element and then "bubbles" upward through the element tree
Bubbling
Event handlers on the event source are invoked
Then routes to successive parent elements until reaching the element tree root Most routed events use bubbling routing strategy
Direct
Only the source element itself is given the opportunity to invoke handlers in response
Tunneling
Event handlers at the tree root are invoked first
Then travels down the object tree to the node that is the source of the event
The element that raised the routed event
Window Grid
Bubbling
StackPanel TextBlock
Commands in .NET
WPF Commands
Commanding is an input mechanism in WPF
Provides input handling at a more semantic level than device input Examples of commands are the Copy, Cut, and Paste operations
purposes
Separate the semantics and the objects that invoke a command from the logic that executes the command
Allows for multiple and disparate sources to invoke the same command logic Allows the command logic to be customized for different targets
WPF Commands
Commands can be used to
indicate whether an
action is available
Example: when trying to cut something, the user should first select something
To indicate whether an action is possible
Implement the CanExecute method
CommandSource
The object that invokes the command
CommandTarget
The object that the command is executed on
CommandBinding
The object that maps command logic to command
Commands in .NET
Live Demo
ICommand Interface
Better Commanding
Even better than Custom Commands
Better Commanding
Most of the times it is not necessary
The RelayCommand
What is a relay command A command which is given an behavior during instantiating Both CanExecute and Execute methods
ICommand someCommand; public MyWindow() { this.someCommand = new RelayCommand(ExecuteMethod,CanExecuteMethod); } public void ExecuteMethod(object parameter) {} public bool CanExecuteMethod(object parameter) {}
Better Commanding
Live Demo
simple:
The Commands can execute without the knowledge of who wants to execute them
Commands are:
Easily implemented
Easily extended Easily replaceable A way to change an object without knowledge of who wants to change it Fundamental part of the MVVM pattern
XAML Concepts
Questions?
Exercises
Extend the VideoPlayer Control from the
example:
Add a slider that slides with the video length Add a slider that changes the volume