WPF 版本 4.5 的新增功能What's New in WPF Version 4.5

本主题包含有关新的和增强功能的信息Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)版本 4.5。This topic contains information about new and enhanced features in Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) version 4.5.

本主题包含以下各节:This topic contains the following sections:

功能区控件Ribbon control

WPF 4.5 附带Ribbon承载快速访问工具栏、 应用程序菜单和选项卡的控件。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) 元素的子集时,将发生 UI 虚拟化。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. VirtualizingPanel定义IsVirtualizingWhenGrouping附加启用 UI 虚拟化分组数据的属性。The VirtualizingPanel defines the IsVirtualizingWhenGrouping attached property that enables UI Virtualization for grouped data. 有关对数据进行分组的详细信息,请参阅如何:排序和分组数据在 XAML 中使用视图。For more information about grouping data, see How to: Sort and Group Data Using a View in XAML. 有关虚拟化的详细信息分组数据,请参阅IsVirtualizingWhenGrouping附加属性。For more information about virtualizing grouped data, see the IsVirtualizingWhenGrouping attached property.

VirtualizingPanel 的新增功能New features for the VirtualizingPanel

  1. 您可以指定是否VirtualizingPanel,如VirtualizingStackPanel,通过使用显示部分项ScrollUnit附加属性。You can specify whether a VirtualizingPanel, such as the VirtualizingStackPanel, displays partial items by using the ScrollUnit attached property. 如果ScrollUnit设置为Item,则VirtualizingPanel将仅显示完全可见的项。If ScrollUnit is set to Item, the VirtualizingPanel will only display items that are completely visible. 如果ScrollUnit设置为Pixel,则VirtualizingPanel可以显示部分可见的项。If ScrollUnit is set to Pixel, the VirtualizingPanel can display partially visible items.

  2. 您可以指定缓存的大小,视区前后时VirtualizingPanel通过使用虚拟化CacheLength附加属性。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. 使用缓存避免生成 UI 元素(因为系统会将它们滚动到视图中)可以提高性能。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. VirtualizingPanel.CacheLengthUnit属性确定使用的度量单位VirtualizingPanel.CacheLengthThe 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. 例如,如果类 SomeClass 定义了名为 MyProperty 的静态属性,则 SomeClass 可以定义 MyProperty 的值发生更改时所引发的静态事件。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;

请注意,在第一种情况下,类将公开名为的静态事件PropertyName Changed通过EventArgs到事件处理程序。Note that in the first case, the class exposes a static event named PropertyNameChanged that passes EventArgs to the event handler. 在第二种情况下,该类会公开名为的静态事件StaticPropertyChanged通过PropertyChangedEventArgs到事件处理程序。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.

访问非 UI 线程上的集合Accessing collections on non-UI Threads

WPF 允许访问和修改线程上的数据集合,创建该集合的线程除外。WPF enables you to access and modify data collections on threads other than the one that created the collection. 这可允许使用后台线程接收来自外部源(例如数据库)的数据,并在 UI 线程上显示该数据。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

INotifyDataErrorInfo接口使数据实体类能够实现自定义验证规则并以异步方式公开验证结果。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. 有关详细信息,请参阅 INotifyDataErrorInfoFor more information, see INotifyDataErrorInfo.

自动更新数据绑定源Automatically updating the source of a data binding

如果使用数据绑定来更新数据源,则可以使用Delay属性来指定要更新源之前对目标属性发生更改后经过的时间量。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. 例如,假设您有Slider具有其Value属性数据双向绑定到数据对象的属性和UpdateSourceTrigger属性设置为PropertyChangedFor 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. 在此示例中,当用户移动Slider,每个像素的源更新的Slider移动。In this example, when the user moves the Slider, the source updates for each pixel that the Slider moves. 源对象通常需要滑块的值时,才滑块的Value停止更改。The source object typically needs the value of the slider only when the slider's Value stops changing. 若要防止对源进行过于频繁更新,请使用Delay指定直到一定的时间结束后在缩略图停止移动不应更新源。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.

绑定到实现 ICustomTypeProvider 的类型Binding to types that Implement ICustomTypeProvider

WPF 支持数据绑定到实现的对象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. 作为PropertyPath在数据绑定。As a PropertyPath in a data binding. 例如,Path属性的Binding可以引用自定义类型的属性。For example, the Path property of a Binding can reference a property of a custom type.

  2. 值作为DataType属性。As the value of the DataType property.

  3. 确定自动生成的列中的类型为DataGridAs a type that determines the automatically generated columns in a DataGrid.

从绑定表达式中检索数据绑定信息Retrieving data binding information from a binding expression

在某些情况下,可能会收到BindingExpressionBinding并且需要有关绑定的源和目标对象的信息。In certain cases, you might get the BindingExpression of a Binding and need information about the source and target objects of the binding. 使用已添加新的 API 可获取源或目标对象或关联的属性。New APIs have been added to enable you to get the source or target object or the associated property. 当你具有BindingExpression,使用以下 Api 来获取有关目标和源的信息。When you have a BindingExpression, use the following APIs to get information about the target and source.

查找绑定的值To find this value of the binding 使用此 APIUse this API
目标对象The target object BindingExpressionBase.Target
目标属性The target property BindingExpressionBase.TargetProperty
源对象The source object BindingExpression.ResolvedSource
源属性The source property BindingExpression.ResolvedSourcePropertyName
是否BindingExpression属于 BindingGroupWhether the BindingExpression belongs to a BindingGroup BindingExpressionBase.BindingGroup
所有者 BindingGroupThe owner of a BindingGroup Owner

检查有效的 DataContext 对象Checking for a valid DataContext object

某些情况下,DataContext中的项容器的ItemsControl断开连接。There are cases where the DataContext of an item container in an ItemsControl becomes disconnected. 项容器是显示中的项的 UI 元素ItemsControlAn item container is the UI element that displays an item in an ItemsControl. 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. 其中删除一个项容器的两种典型情况是从基础集合删除项的时间和当在上启用虚拟化ItemsControlTwo 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. 在这些情况下,DataContext项容器的属性将设置为返回的 sentinel 对象BindingOperations.DisconnectedSource静态属性。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. 应检查是否DataContext等同于DisconnectedSource然后才能访问对象DataContext的项容器。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 允许在修改数据时重新排列数据。WPF 4.5 enables the data to be rearranged when the data is modified. 例如,假设应用程序使用DataGrid列出股票市场中的股票和股票按股票值。For example, suppose that an application uses a DataGrid to list stocks in a stock market and the stocks are sorted by stock value. 如果在股票的上启用实时排序CollectionView中的股票的位置DataGrid移动时的股票值将成为更高版本或小于另一个股票的值。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. 有关详细信息,请参阅ICollectionViewLiveShaping接口。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. 泛型WeakEventManager类还可使订阅服务器参与弱事件模式,如果专用WeakEventManager不存在特定事件。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. 同步Invoke方法定义采用重载ActionFunc<TResult>参数。The synchronous Invoke method defines overloads that take an Action or Func<TResult> parameter. 新的异步方法, InvokeAsync,还会ActionFunc<TResult>作为回调参数并返回DispatcherOperationDispatcherOperation<TResult>The new asynchronous method, InvokeAsync, also takes an Action or Func<TResult> as the callback parameter and returns a DispatcherOperation or DispatcherOperation<TResult>. DispatcherOperationDispatcherOperation<TResult>类定义Task属性。The DispatcherOperation and DispatcherOperation<TResult> classes define a Task property. 当您调用InvokeAsync,可以使用await使用的关键字DispatcherOperation或关联TaskWhen you call InvokeAsync, you can use the await keyword with either the DispatcherOperation or the associated Task. 如果你需要同步等待Task返回的DispatcherOperationDispatcherOperation<TResult>,调用DispatcherOperationWait扩展方法。If you need to wait synchronously for the Task that is returned by a DispatcherOperation or DispatcherOperation<TResult>, call the DispatcherOperationWait extension method. 调用Task.Wait在调用线程上排队操作将导致死锁。Calling Task.Wait will result in a deadlock if the operation is queued on a calling thread. 有关使用详细信息Task若要执行异步操作,请参阅任务并行 (任务并行库)For more information about using a Task to perform asynchronous operations, see Task Parallelism (Task Parallel Library).

事件的标记扩展Markup Extensions for Events

WPF 4.5 支持事件的标记扩展。WPF 4.5 supports markup extensions for events. 虽然 WPF 未定义用于事件的标记扩展,但第三方能够创建可与事件配合使用的标记扩展。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