Binding Binding Binding Binding Class

定义

提供对绑定定义的高级访问,该绑定连接绑定目标对象(通常为 WPF 元素)的属性和任何数据源(例如数据库、XML 文件,或包含数据的任何对象)。Provides high-level access to the definition of a binding, which connects the properties of binding target objects (typically, WPF elements), and any data source (for example, a database, an XML file, or any object that contains data).

public ref class Binding : System::Windows::Data::BindingBase
public class Binding : System.Windows.Data.BindingBase
type Binding = class
    inherit BindingBase
Public Class Binding
Inherits BindingBase
继承

注解

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 数据绑定为应用程序呈现数据并与数据交互提供了一种简单且一致的方式。data binding provides a simple and consistent way for applications to present and interact with data. 数据绑定使您可以同步两个不同对象的属性值。Data binding enables you to synchronize the values of the properties of two different objects.

若要建立绑定, 请使用Binding类或继承BindingBase的其他类之一。To establish a binding, use the Binding class or one of the other classes that inherit BindingBase. 不管要绑定的对象和数据源的性质如何, 每个绑定都遵循下图所示的模型。Regardless of what object you are binding and the nature of your data source, each binding follows the model illustrated by the following figure.

基本数据绑定关系图Basic data binding diagram

下图演示了下面的WPFWPF基本数据绑定概念。The figure demonstrates the following fundamental WPFWPF data binding concepts.

  • 每个绑定通常具有以下四个组件: 绑定目标对象、目标属性、绑定源和Path绑定源中要使用的值。Each binding typically has these four components: a binding target object, a target property, a binding source, and a Path to the value in the binding source to use. 例如, 如果你想要将的内容TextBox绑定到 Employee 对象的 Name 属性, 则目标对象TextBox为, 目标属性是Text属性, 要使用的值为 Name, 并且源对象为 Employee对象.For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.

  • 目标属性必须为依赖属性。The target property must be a dependency property. 这也意味着无法绑定字段。This also means that you cannot bind a field. 对象的UIElement大多数属性都是依赖属性, 大多数依赖属性 (只读属性除外) 默认支持数据绑定。Most properties of UIElement objects are dependency properties and most dependency properties, except read-only ones, support data binding by default. (仅DependencyObject类型可以定义依赖属性, 所有UIElement对象均派生DependencyObject自。)(Only DependencyObject types can define dependency properties and all UIElement objects derive from DependencyObject.)

  • 尽管图中并未指出,但请注意,绑定源对象并不限于自定义 CLRCLR 对象。Although not specified in the figure, it should be noted that the binding source object is not restricted to being a custom CLRCLR object. WPFWPF 数据绑定支持 CLRCLR 对象和 XMLXML 形式的数据。data binding supports data in the form of CLRCLR objects and XMLXML. 若要提供一些示例, 绑定源可以UIElement是、任何列表对象CLRCLR 、与ADO.NETADO.NET数据或 Web XMLXML服务相关联的对象, 或者是包含数据的 XmlNode。To provide some examples, your binding source may be a UIElement, any list object, a CLRCLR object that is associated with ADO.NETADO.NET data or Web Services, or an XmlNode that contains your XMLXML data.

使用 " Mode属性" 指定数据流的方向。Use the Mode property to specify the direction of the data flow. 若要检测单向或双向绑定中的源更改, 源必须实现适当的属性更改通知机制, 例如INotifyPropertyChangedTo detect source changes in one-way or two-way bindings, the source must implement a suitable property change notification mechanism such as INotifyPropertyChanged. 有关示例,请参见 如何:实现属性更改通知For an example, see How to: Implement Property Change Notification. UpdateSourceTrigger属性指定源更新的时间。The UpdateSourceTrigger property specifies the timing of source updates. 有关详细信息, 请参阅数据绑定概述中的 "基本数据绑定概念"。For more information, see "Basic Data Binding Concepts" in Data Binding Overview.

XAML 属性用法XAML Attribute Usage

<object property="{Binding  declaration}"/>  

XAML 值XAML Values

declarationdeclaration
零个或多个由逗号 (,) 分隔的属性赋值子句。Zero or more attribute-assignment clauses separated by commas (,). 有关详细信息, 请参阅绑定标记扩展绑定声明概述For more information, see Binding Markup Extension or Binding Declarations Overview.

构造函数

Binding() Binding() Binding() Binding()

初始化 Binding 类的新实例。Initializes a new instance of the Binding class.

Binding(String) Binding(String) Binding(String) Binding(String)

使用初始路径初始化 Binding 类的新实例。Initializes a new instance of the Binding class with an initial path.

字段

DoNothing DoNothing DoNothing DoNothing

用作返回值以指示绑定引擎不执行任何操作。Used as a returned value to instruct the binding engine not to perform any action.

IndexerName IndexerName IndexerName IndexerName

用作 PropertyNamePropertyChangedEventArgs 以指示索引器属性已更改。Used as the PropertyName of PropertyChangedEventArgs to indicate that an indexer property has changed.

SourceUpdatedEvent SourceUpdatedEvent SourceUpdatedEvent SourceUpdatedEvent

标识 SourceUpdated 附加事件。Identifies the SourceUpdated attached event.

TargetUpdatedEvent TargetUpdatedEvent TargetUpdatedEvent TargetUpdatedEvent

标识 TargetUpdated 附加事件。Identifies the TargetUpdated attached event.

XmlNamespaceManagerProperty XmlNamespaceManagerProperty XmlNamespaceManagerProperty XmlNamespaceManagerProperty

标识 XmlNamespaceManager 附加属性。Identifies the XmlNamespaceManager attached property.

属性

AsyncState AsyncState AsyncState AsyncState

获取或设置传递给异步数据调度程序的不透明数据。Gets or sets opaque data passed to the asynchronous data dispatcher.

BindingGroupName BindingGroupName BindingGroupName BindingGroupName

获取或设置此绑定所属的 BindingGroup 的名称。Gets or sets the name of the BindingGroup to which this binding belongs.

(Inherited from BindingBase)
BindsDirectlyToSource BindsDirectlyToSource BindsDirectlyToSource BindsDirectlyToSource

获取或设置一个值,该值指示是相对于数据项还是 DataSourceProvider 对象计算 PathGets or sets a value that indicates whether to evaluate the Path relative to the data item or the DataSourceProvider object.

Converter Converter Converter Converter

获取或设置要使用的转换器。Gets or sets the converter to use.

ConverterCulture ConverterCulture ConverterCulture ConverterCulture

获取或设置计算转换器要使用的区域性。Gets or sets the culture in which to evaluate the converter.

ConverterParameter ConverterParameter ConverterParameter ConverterParameter

获取或设置要传递给 Converter 的参数。Gets or sets the parameter to pass to the Converter.

Delay Delay Delay Delay

获取或设置更新位于目标更改上的值之后的绑定源前要等待的时间(毫秒)。Gets or sets the amount of time, in milliseconds, to wait before updating the binding source after the value on the target changes.

(Inherited from BindingBase)
ElementName ElementName ElementName ElementName

获取或设置要用作绑定源对象的元素的名称。Gets or sets the name of the element to use as the binding source object.

FallbackValue FallbackValue FallbackValue FallbackValue

获取或设置当绑定无法返回值时要使用的值。Gets or sets the value to use when the binding is unable to return a value.

(Inherited from BindingBase)
IsAsync IsAsync IsAsync IsAsync

获取或设置一个值,该值表示 Binding 是否应异步获取和设置值。Gets or sets a value that indicates whether the Binding should get and set values asynchronously.

Mode Mode Mode Mode

获取或设置一个值,该值指示绑定的数据流方向。Gets or sets a value that indicates the direction of the data flow in the binding.

NotifyOnSourceUpdated NotifyOnSourceUpdated NotifyOnSourceUpdated NotifyOnSourceUpdated

获取或设置一个值,该值指示当值从绑定目标传输到绑定源时是否引发 SourceUpdated 事件。Gets or sets a value that indicates whether to raise the SourceUpdated event when a value is transferred from the binding target to the binding source.

NotifyOnTargetUpdated NotifyOnTargetUpdated NotifyOnTargetUpdated NotifyOnTargetUpdated

获取或设置一个值,该值指示当值从绑定源传输到绑定目标时是否引发 TargetUpdated 事件。Gets or sets a value that indicates whether to raise the TargetUpdated event when a value is transferred from the binding source to the binding target.

NotifyOnValidationError NotifyOnValidationError NotifyOnValidationError NotifyOnValidationError

获取或设置一个值,该值指示是否对绑定对象引发 Error 附加事件。Gets or sets a value that indicates whether to raise the Error attached event on the bound object.

Path Path Path Path

获取或设置绑定源属性的路径。Gets or sets the path to the binding source property.

RelativeSource RelativeSource RelativeSource RelativeSource

通过指定绑定源相对于绑定目标位置的位置,获取或设置此绑定源。Gets or sets the binding source by specifying its location relative to the position of the binding target.

Source Source Source Source

获取或设置要用作绑定源的对象。Gets or sets the object to use as the binding source.

StringFormat StringFormat StringFormat StringFormat

获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式。Gets or sets a string that specifies how to format the binding if it displays the bound value as a string.

(Inherited from BindingBase)
TargetNullValue TargetNullValue TargetNullValue TargetNullValue

获取或设置当源的值为 null 时在目标中使用的值。Gets or sets the value that is used in the target when the value of the source is null.

(Inherited from BindingBase)
UpdateSourceExceptionFilter UpdateSourceExceptionFilter UpdateSourceExceptionFilter UpdateSourceExceptionFilter

获取或设置一个处理程序,可使用该程序提供自定义逻辑,旨在在更新绑定源值期间处理绑定引擎遇到的异常。Gets or sets a handler you can use to provide custom logic for handling exceptions that the binding engine encounters during the update of the binding source value. 这仅适用于你已将 ExceptionValidationRule 与绑定相关联的情况。This is only applicable if you have associated an ExceptionValidationRule with your binding.

UpdateSourceTrigger UpdateSourceTrigger UpdateSourceTrigger UpdateSourceTrigger

获取或设置一个值,它可确定绑定源更新的计时。Gets or sets a value that determines the timing of binding source updates.

ValidatesOnDataErrors ValidatesOnDataErrors ValidatesOnDataErrors ValidatesOnDataErrors

获取或设置一个值,该值指示是否包含 DataErrorValidationRuleGets or sets a value that indicates whether to include the DataErrorValidationRule.

ValidatesOnExceptions ValidatesOnExceptions ValidatesOnExceptions ValidatesOnExceptions

获取或设置一个值,该值指示是否包含 ExceptionValidationRuleGets or sets a value that indicates whether to include the ExceptionValidationRule.

ValidatesOnNotifyDataErrors ValidatesOnNotifyDataErrors ValidatesOnNotifyDataErrors ValidatesOnNotifyDataErrors

获取或设置一个值,该值指示是否包含 NotifyDataErrorValidationRuleGets or sets a value that indicates whether to include the NotifyDataErrorValidationRule.

ValidationRules ValidationRules ValidationRules ValidationRules

获取用于检查用户输入有效性的规则的集合。Gets a collection of rules that check the validity of the user input.

XPath XPath XPath XPath

获取或设置 XPath 查询,该查询返回要使用的 XMLXML 绑定源上的值。Gets or sets an XPath query that returns the value on the XMLXML binding source to use.

附加属性

XmlNamespaceManager XmlNamespaceManager XmlNamespaceManager XmlNamespaceManager

获取或设置用于在 XML 绑定中执行可以识别命名空间的 XPath 查询的 XmlNamespaceManagerGets or sets the XmlNamespaceManager used to perform namespace-aware XPath queries in XML bindings.

方法

AddSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) AddSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) AddSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) AddSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

SourceUpdated 附加事件添加处理程序。Adds a handler for the SourceUpdated attached event.

AddTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) AddTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) AddTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) AddTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

TargetUpdated 附加事件添加处理程序。Adds a handler for the TargetUpdated attached event.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
GetXmlNamespaceManager(DependencyObject) GetXmlNamespaceManager(DependencyObject) GetXmlNamespaceManager(DependencyObject) GetXmlNamespaceManager(DependencyObject)

返回附加到指定对象的绑定所使用的 XMLXML 命名空间管理器对象。Returns an XMLXML namespace manager object used by the binding attached to the specified object.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ProvideValue(IServiceProvider) ProvideValue(IServiceProvider) ProvideValue(IServiceProvider) ProvideValue(IServiceProvider)

返回一个应在应用了此绑定和扩展的属性上设置的对象。Returns an object that should be set on the property where this binding and extension are applied.

(Inherited from BindingBase)
RemoveSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) RemoveSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) RemoveSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) RemoveSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

移除 SourceUpdated 附加事件的处理程序。Removes a handler for the SourceUpdated attached event.

RemoveTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) RemoveTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) RemoveTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>) RemoveTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

移除 TargetUpdated 附加事件的处理程序。Removes a handler for the TargetUpdated attached event.

SetXmlNamespaceManager(DependencyObject, XmlNamespaceManager) SetXmlNamespaceManager(DependencyObject, XmlNamespaceManager) SetXmlNamespaceManager(DependencyObject, XmlNamespaceManager) SetXmlNamespaceManager(DependencyObject, XmlNamespaceManager)

设置被附加到提供的元素的绑定所使用的命名空间管理器对象。Sets a namespace manager object used by the binding attached to the provided element.

ShouldSerializeFallbackValue() ShouldSerializeFallbackValue() ShouldSerializeFallbackValue() ShouldSerializeFallbackValue()

返回一个值,该值指示序列化进程是否应当对此类的实例的 FallbackValue 有效属性值进行序列化。Returns a value that indicates whether serialization processes should serialize the effective value of the FallbackValue property on instances of this class.

(Inherited from BindingBase)
ShouldSerializePath() ShouldSerializePath() ShouldSerializePath() ShouldSerializePath()

指示是否应使 Path 属性持久化。Indicates whether the Path property should be persisted.

ShouldSerializeSource() ShouldSerializeSource() ShouldSerializeSource() ShouldSerializeSource()

指示是否应使 Source 属性持久化。Indicates whether the Source property should be persisted.

ShouldSerializeTargetNullValue() ShouldSerializeTargetNullValue() ShouldSerializeTargetNullValue() ShouldSerializeTargetNullValue()

返回一个值,该值指示是否应序列化 TargetNullValue 属性。Returns a value that indicates whether the TargetNullValue property should be serialized.

(Inherited from BindingBase)
ShouldSerializeValidationRules() ShouldSerializeValidationRules() ShouldSerializeValidationRules() ShouldSerializeValidationRules()

指示是否应使 ValidationRules 属性持久化。Indicates whether the ValidationRules property should be persisted.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

附加事件

SourceUpdated SourceUpdated SourceUpdated SourceUpdated

当一个值从绑定目标传输到绑定源时发生,但仅限于其 NotifyOnSourceUpdated 值设置为 true 的绑定。Occurs when a value is transferred from the binding target to the binding source, but only for bindings with the NotifyOnSourceUpdated value set to true.

TargetUpdated TargetUpdated TargetUpdated TargetUpdated

当一个值从绑定源传输到绑定目标时发生,但仅限于其 NotifyOnTargetUpdated 值设置为 true 的绑定。Occurs when a value is transferred from the binding source to the binding target, but only for bindings with the NotifyOnTargetUpdated value set to true.

适用于

另请参阅