Redigera

Dela via


What's New in WPF Version 4.5

This topic contains information about new and enhanced features in Windows Presentation Foundation (WPF) version 4.5.

This topic contains the following sections:

Ribbon control

WPF 4.5 ships with a Ribbon control that hosts a Quick Access Toolbar, Application Menu, and tabs. For more information, see the Ribbon Overview.

Improved performance when displaying large sets of grouped data

UI virtualization occurs when a subset of user interface (UI) elements are generated from a larger number of data items based on which items are visible on the screen. The VirtualizingPanel defines the IsVirtualizingWhenGrouping attached property that enables UI Virtualization for grouped data. For more information about grouping data, see How to: Sort and Group Data Using a View in XAML. For more information about virtualizing grouped data, see the IsVirtualizingWhenGrouping attached property.

New features for the VirtualizingPanel

  1. You can specify whether a VirtualizingPanel, such as the VirtualizingStackPanel, displays partial items by using the ScrollUnit attached property. If ScrollUnit is set to Item, the VirtualizingPanel will only display items that are completely visible. If ScrollUnit is set to Pixel, the VirtualizingPanel can display partially visible items.

  2. You can specify the size of the cache before and after the viewport when the VirtualizingPanel is virtualizing by using the CacheLength attached property. The cache is the amount of space above or below the viewport in which items are not virtualized. Using a cache to avoid generating UI elements as they’re scrolled into view can improve performance. The cache is populated at a lower priority so that the application does not become unresponsive during the operation. The VirtualizingPanel.CacheLengthUnit property determines the unit of measurement that is used by VirtualizingPanel.CacheLength.

Binding to static properties

You can use static properties as the source of a data binding. The data binding engine recognizes when the property's value changes if a static event is raised. For example, if the class SomeClass defines a static property called MyProperty, SomeClass can define a static event that is raised when the value of MyProperty changes. The static event can use either of the following signatures.

  • public static event EventHandler MyPropertyChanged;

  • public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

Note that in the first case, the class exposes a static event named PropertyNameChanged that passes EventArgs to the event handler. In the second case, the class exposes a static event named StaticPropertyChanged that passes PropertyChangedEventArgs to the event handler. A class that implements the static property can choose to raise property-change notifications using either method.

Accessing collections on non-UI Threads

WPF enables you to access and modify data collections on threads other than the one that created the collection. This enables you to use a background thread to receive data from an external source, such as a database, and display the data on the UI thread. By using another thread to modify the collection, your user interface remains responsive to user interaction.

Synchronously and Asynchronously validating data

The INotifyDataErrorInfo interface enables data entity classes to implement custom validation rules and expose validation results asynchronously. This interface also supports custom error objects, multiple errors per property, cross-property errors, and entity-level errors. For more information, see INotifyDataErrorInfo.

Automatically updating the source of a data binding

If you use a data binding to update a data source, you can use the Delay property to specify an amount of time to pass after the property changes on the target before the source updates. For example, suppose that you have a Slider that has its Value property data two-way bound to a property of a data object and the UpdateSourceTrigger property is set to PropertyChanged. In this example, when the user moves the Slider, the source updates for each pixel that the Slider moves. The source object typically needs the value of the slider only when the slider's Value stops changing. To prevent updating the source too often, use Delay to specify that the source should not be updated until a certain amount of time elapses after the thumb stops moving.

Binding to types that Implement ICustomTypeProvider

WPF supports data binding to objects that implement ICustomTypeProvider, also known as custom types. You can use custom types in the following cases.

  1. As a PropertyPath in a data binding. For example, the Path property of a Binding can reference a property of a custom type.

  2. As the value of the DataType property.

  3. As a type that determines the automatically generated columns in a DataGrid.

Retrieving data binding information from a binding expression

In certain cases, you might get the BindingExpression of a Binding and need information about the source and target objects of the binding. New APIs have been added to enable you to get the source or target object or the associated property. When you have a BindingExpression, use the following APIs to get information about the target and source.

To find this value of the binding Use this API
The target object BindingExpressionBase.Target
The target property BindingExpressionBase.TargetProperty
The source object BindingExpression.ResolvedSource
The source property BindingExpression.ResolvedSourcePropertyName
Whether the BindingExpression belongs to a BindingGroup BindingExpressionBase.BindingGroup
The owner of a BindingGroup Owner

Checking for a valid DataContext object

There are cases where the DataContext of an item container in an ItemsControl becomes disconnected. An item container is the UI element that displays an item in an ItemsControl. When an ItemsControl is data bound to a collection, an item container is generated for each item. In some cases, item containers are removed from the visual tree. Two typical cases where an item container is removed are when an item is removed from the underlying collection and when virtualization is enabled on the ItemsControl. In these cases, the DataContext property of the item container will be set to the sentinel object that is returned by the BindingOperations.DisconnectedSource static property. You should check whether the DataContext is equal to the DisconnectedSource object before accessing the DataContext of an item container.

Repositioning data as the data's values change (Live shaping)

A collection of data can be grouped, sorted, or filtered. WPF 4.5 enables the data to be rearranged when the data is modified. For example, suppose that an application uses a DataGrid to list stocks in a stock market and the stocks are sorted by stock value. If live sorting is enabled on the stocks' CollectionView, a stock's position in the DataGrid moves when the value of the stock becomes greater or less than another stock's value. For more information, see the ICollectionViewLiveShaping interface.

Improved Support for Establishing a Weak Reference to an Event

Implementing the weak event pattern is now easier because subscribers to events can participate in it without implementing an extra interface. The generic WeakEventManager class also enables subscribers to participate in the weak event pattern if a dedicated WeakEventManager does not exist for a certain event. For more information, see Weak Event Patterns.

New methods for the Dispatcher class

The Dispatcher class defines new methods for synchronous and asynchronous operations. The synchronous Invoke method defines overloads that take an Action or Func<TResult> parameter. The new asynchronous method, InvokeAsync, also takes an Action or Func<TResult> as the callback parameter and returns a DispatcherOperation or DispatcherOperation<TResult>. The DispatcherOperation and DispatcherOperation<TResult> classes define a Task property. When you call InvokeAsync, you can use the await keyword with either the DispatcherOperation or the associated Task. If you need to wait synchronously for the Task that is returned by a DispatcherOperation or DispatcherOperation<TResult>, call the DispatcherOperationWait extension method. Calling Task.Wait will result in a deadlock if the operation is queued on a calling thread. For more information about using a Task to perform asynchronous operations, see Task Parallelism (Task Parallel Library).

Markup Extensions for Events

WPF 4.5 supports markup extensions for events. While WPF does not define a markup extension to be used for events, third parties are able to create a markup extension that can be used with events.

See also