FrameworkElement.SetBinding 方法

定义

为指定的依赖属性向此元素附加一个绑定。

重载

SetBinding(DependencyProperty, String)

将提供的源属性名作为数据源的路径限定,从而将绑定附加到此元素上。

SetBinding(DependencyProperty, BindingBase)

基于已提供的绑定对象将一个绑定附加到此元素上。

SetBinding(DependencyProperty, String)

将提供的源属性名作为数据源的路径限定,从而将绑定附加到此元素上。

C#
public System.Windows.Data.BindingExpression SetBinding (System.Windows.DependencyProperty dp, string path);

参数

dp
DependencyProperty

标识应该建立绑定的目标属性。

path
String

源属性名称或用于绑定的属性的路径。

返回

BindingExpression

记录绑定的条件。 此返回值可用于错误检查。

示例

以下示例使用特定路径设置绑定。

C#
myDataObject = new MyData(System.DateTime.Now);
root.DataContext = myDataObject;
myText.SetBinding(TextBlock.TextProperty, "MyDataProperty");

注解

此方法是用于调用BindingOperations.SetBinding的便利方法,该方法将当前实例作为DependencyObject该实例传递,并基于提供的path参数创建新Binding实例。 如果建立简单的默认绑定,则此签名更加方便。 如果需要将任何绑定属性指定为非默认条件,或者想要使用MultiBindingPriorityBinding或,则应使用SetBinding(DependencyProperty, BindingBase)签名。

适用于

.NET Framework 4.8 和其他版本
产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

SetBinding(DependencyProperty, BindingBase)

基于已提供的绑定对象将一个绑定附加到此元素上。

C#
public System.Windows.Data.BindingExpressionBase SetBinding (System.Windows.DependencyProperty dp, System.Windows.Data.BindingBase binding);

参数

dp
DependencyProperty

标识应在其中建立绑定的属性。

binding
BindingBase

表示数据绑定的详细信息。

返回

BindingExpressionBase

记录绑定的条件。 此返回值可用于错误检查。

注解

此方法是用于调用 BindingOperations.SetBinding的便利方法,该方法将当前实例作为调用 DependencyObject传递。

适用于

.NET Framework 4.8 和其他版本
产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7