Binding 类

定义

定义一个绑定,该绑定连接绑定目标和数据源的属性。

/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Binding : BindingBase
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class Binding : BindingBase
Public Class Binding
Inherits BindingBase
<Binding .../>
- or -
<dependencyobject dependencyproperty="{Binding bindingArgs}" />
继承
Object IInspectable DependencyObject BindingBase Binding
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

下面的代码示例演示如何在 XAML 中创建绑定。 有关完整的代码列表,请参阅 XAML 数据绑定示例

<StackPanel Margin="5">

  <TextBlock Text="Name:" Style="{StaticResource DescriptionTextStyle}" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <TextBox Text="{Binding Path=Name, Mode=TwoWay}" 
    Width="350" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <TextBlock Text="Organization:" Style="{StaticResource DescriptionTextStyle}" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <!-- You can omit the 'Path=' portion of the binding expression. --> 
  <TextBox Text="{Binding Organization, Mode=TwoWay}" Width="350" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

</StackPanel>

以下示例代码演示如何在代码中创建绑定。

// Create the source string.
string s = "Hello";

// Create the binding description.
Binding b = new Binding();
b.Mode = BindingMode.OneTime;
b.Source = s;

// Attach the binding to the target.
TextBlock MyText = new TextBlock();
MyText.SetBinding(TextBlock.TextProperty, b);
// Create the source string.
String^ s = ref new String(L"Hello");

// Create the binding description.
Binding^ b = ref new Binding();
b->Mode = BindingMode::OneTime;
b->Source = s;

// Attach the binding to the target.
TextBlock^ MyText = ref new TextBlock();
MyText->SetBinding(MyText->TextProperty, b);
'Create the source string 
Dim s As String = "Hello"

'Create the binding description 
Dim b As New Binding()
b.Mode = BindingMode.OneTime
b.Source = s

'Attach the binding to the target 
Dim MyText As New TextBlock()
MyText.SetBinding(TextBlock.TextProperty, b)

注解

{Binding} 标记扩展允许您在 XAML 中将 Binding 值指定为单个属性字符串,包括设置 Binding 属性,例如 PathSource。 有关数据绑定概念的详细信息,请参阅 深入的数据绑定

Binding 类可能被视为 {Binding} 标记扩展的代码隐藏公开。 如果绑定已应用于目标 () 加载 XAML 时发生,则无法设置 Binding 对象的读写属性来更改绑定在运行时的行为方式。 任何 XAML 定义的绑定都应被视为不可变。 但是,可以使用 FrameworkElement.SetBinding 创建新的 Binding 对象,设置其属性,并在特定 UI 元素目标上建立新绑定。 有关详细信息,请参阅 在代码中创建绑定

Binding 对象将 FrameworkElement 的依赖属性直接连接到数据对象,以便数据对象的更新自动传播到使用数据绑定的属性。 Binding 类定义绑定的属性。 每个绑定都必须具有目标元素、目标属性和数据源,但如果未指定这些值,则会默认提供一些值。

若要绑定到数据对象的属性或子属性,请设置 Binding 对象的 Path 属性。 有关如何在代码或 XAML 中设置 Path 的详细信息,请参阅 属性路径语法{Binding} 标记扩展

可以将 Binding 类的实例应用于多个目标。 但是,在将 Binding 对象附加到目标元素后,无法修改该对象的属性值。

注意

调用 FrameworkElement.SetBinding 方法并传入新的 Binding 对象不一定删除现有绑定。 应改用 DependencyObject.ClearValue 方法。

有关可采用绑定或可设置为数据绑定值的属性的 XAML 属性用法的详细信息,请参阅 {Binding} 标记扩展

作为数据绑定目标的属性必须是依赖属性。 有关详细信息,请参阅依赖属性概述

构造函数

Binding()

初始化 Binding 类的新实例。

属性

Converter

获取或设置绑定引擎调用的转换器对象,以在源和目标之间传递数据时修改数据,反之亦然。

ConverterLanguage

获取或设置一个值,该值命名要传递给 Converter 属性指定的任何转换器的语言。

ConverterParameter

获取或设置可在 转换器 逻辑中使用的参数。

Dispatcher

获取与此 对象关联的 CoreDispatcherCoreDispatcher 表示可以访问 UI 线程上的 DependencyObject 的工具,即使代码是由非 UI 线程启动的。

(继承自 DependencyObject)
ElementName

获取或设置要用作 Binding 的绑定源的元素的名称。

FallbackValue

获取或设置当绑定无法返回值时要使用的值。

Mode

获取或设置一个值,该值指示绑定的数据流方向。

Path

获取或设置绑定源属性的路径。

RelativeSource

通过指定绑定源相对于绑定目标位置的位置,获取或设置此绑定源。 这在 XAML 控件模板内的绑定中最常用。

Source

获取或设置绑定的数据源。

TargetNullValue

获取或设置源的值为 null 时在目标中使用的值。

UpdateSourceTrigger

获取或设置一个值,该值确定双向绑定的绑定源更新的计时。

方法

ClearValue(DependencyProperty)

清除依赖属性的本地值。

(继承自 DependencyObject)
GetAnimationBaseValue(DependencyProperty)

返回为依赖属性建立的任何基值,该基值适用于动画未处于活动状态的情况。

(继承自 DependencyObject)
GetValue(DependencyProperty)

DependencyObject 返回依赖属性的当前有效值。

(继承自 DependencyObject)
ReadLocalValue(DependencyProperty)

如果设置了本地值,则返回依赖属性的本地值。

(继承自 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

注册一个通知函数,用于侦听此 DependencyObject 实例上特定 DependencyProperty 的更改。

(继承自 DependencyObject)
SetValue(DependencyProperty, Object)

设置 DependencyObject 上依赖属性的本地值。

(继承自 DependencyObject)
UnregisterPropertyChangedCallback(DependencyProperty, Int64)

取消以前通过调用 RegisterPropertyChangedCallback 注册的更改通知。

(继承自 DependencyObject)

适用于

另请参阅