280 likes | 303 Views
This chapter introduces important concepts beyond .NET programming in WPF, covering logical and visual trees, dependency properties, routed events, and commands. Learn how to utilize these extended concepts to create sophisticated UI applications.
E N D
Important New Concepts In WPF Orlando Robertson Brown Bag Seminar Windows Presentation Foundation Unleashed
Introduction • This chapter introduces concepts beyond .NET programming • New Concepts • Logical Trees • Visual Trees • Extended Concepts • Dependency Properties • Routed Events • Commands • Review UI applications needed for programming
Logical & Visual Trees • Logical Trees are objects to create UIs in WPF • Important because every aspect in WPF is associated to Logical Trees • Visual Trees are considered an expanded logical tree exposing visual components • Visual Trees expose elements derived from System.Windows.Media.Visual &Visual3D
Logical & Visual Trees • Visual Trees & Logical Trees are needed for applications separating look and logic • Logical Trees can be traversed using class System.Windows.LogicalTreeHelper • Visual Trees can be traversed using class System.Windows.VisualTreeHelper
Dependency Properties • Dependency Property is a new WPF property type used to enable styling, automatic data binding, animation • Dependency Properties provide change notifications from declarative markup • Also property value inheritance & multiple provider support • Styling and Animation are done with dependency properties
A Dependency Property Implementation • Dependency Properties are .NET properties connected to extra WPF infrastructure • Represented by System.Windows.DependencyProperty • Created by calling DependencyProperty.Register • Options for passing metadata, callbacks, coercing values, validating values • .NET property wrappers are bypassed at run-time when setting Dependency Properties in XAML
Change Notification • Change Notifications allows WPF to automatically trigger actions when dependency properties change • Property Trigger, Data Trigger, Event Trigger • Property Trigger allows custom changes when property values change without procedural code • Data Trigger works for all .NET properties, not just dependency properties • Event Trigger declaratively specifies actions for raised events • Used for animation and sound applications
Property Value Inheritance • Property Value Inheritance refers to flow of property values down the element tree • Child elements inherit properties of the parent element unless overridden • Elements deriving from Freezable may inherit values from the parent element
Support for Multiple Providers • WPF executes a 5 step process to calculate the final value of dependency properties using change notifications • Step 1 Determine Base Value • Applies a precedence order for property providers • Step 2 Evaluate • Evaluate expressions from step 1 • Step 3 Apply Animations • Apply animation values to current properties
Support for Multiple Providers • Step 4 Coerce • Passes the resulting property values to a CoerceValueCallback delegate • Step 5 Validate • Passes the coerced property values to a ValidateValueCallback delegate
Attached Properties • Attached Properties are special dependency properties assigned to arbitrary objects • Objects that don’t have a dependency property of it’s own can be attached and used for property value inheritance • Ex. Assigning a FontSize to a StackPanel
Routed Events • Routed Events traverse thru visual and logical trees without the need for custom code • Provides the service of encapsulation of application from visual trees • Allows for embedded complex content and visual trees
A Routed Event Implementation • Routed Events are static public fields • Routed Events are represented by RoutedEvents • Created by calling EventManger.RegisterRoutedEvent
Routing Strategies & Event Handlers • RoutingStrategy enumeration: Tunneling, Bubbling, Direct • Tunneling raises the routed event at the root then to each element toward the source element • Bubbling raises the routed event at the source element then to each element toward the root • Direct raises the routed event at the source element only
Routing Strategies & Event Handlers • Routed Event handlers are similar to .NET event handlers • 1st Parameter is System.Object sender • 2nd Parameter is RoutedEventArgs e • RoutedEventArgs derived from EventArgs
Routed Events in Action • UIElement class defines routed events for keyboard, mouse, stylus input • Many of the bubbling events are paired with tunneling events • Naming convention of tunneling events are prefixed with Preview • Tunneling Event: PreviewMouseMove • Bubbling Event: MouseMove • Event pairing provides the option of canceling or modifying a bubbling event
Attached Events • Attached Events provide tunneling and bubbling of routed events to elements that don’t define the event • Similar to attached properties • Routed events can be used as attached events
Commands • Commands represent independent actions from the UI • Multiple commands can be exposed with the use of events • WPF reduces the amount of code needed to support commands • WPF provides flexibility to change UIs supporting commands without breaking back-end logic
Built-In Commands • WPF defines many commands ready for use • ApplicationCommands: Cut, Paste • ComponentCommands: MoveUp, MoveDown • MediaCommands: ChannelUp, ChannelDown • NavigationCommands: Search, Refresh • EditingCommands: AlignLeft, AlignCenter • These commands are instances of RoutedUICommand • Implements ICommand & Supports Bubbling
Executing Commands w/ Input Gestures • Input Gestures can be bound to commands by adding a KeyBinding or MouseBinding to the elements InputBindings collection • Ex Help command provides auto binding to keyboard shortcuts • Default: F1 Key • Input Gestures can assign F2 key to execute Help command
Controls w/ Built-In Command Bindings • Some controls in WPF have their own command bindings • Ex TextBox control bound to Cut, Paste • Built-In Commands can be bound by WPF controls • Button bound to a TextBox Built-In command • WPF’s built-in commands becomes important • Buttons and TextBoxes can gain rich interaction
Conclusion • We learned the major WPF applications for the .NET Framework • Reviewed types of properties, types of events, trees, declarative code, procedural code • We now have an understanding of WPF controls and developing WPF UIs.
References • “Windows Presentation Foundation Unleashed”, Adam Nathan with Daniel Lehenbauer • Software Requirements • Windows that support .NET 3.0: XP SP2, Vista • .NET Framework 3.0 • Software Recommended • Windows SDK (http://msdn.com) • Visual Studio 2005 or later