BindingOperations.GetBindingExpression(DependencyObject, DependencyProperty) 方法


返回与指定对象上的指定的绑定目标关联的 BindingExpression 对象。Returns the BindingExpression object associated with the specified binding target property on the specified object.

 static System::Windows::Data::BindingExpression ^ GetBindingExpression(System::Windows::DependencyObject ^ target, System::Windows::DependencyProperty ^ dp);
public static System.Windows.Data.BindingExpression GetBindingExpression (System.Windows.DependencyObject target, System.Windows.DependencyProperty dp);
static member GetBindingExpression : System.Windows.DependencyObject * System.Windows.DependencyProperty -> System.Windows.Data.BindingExpression
Public Shared Function GetBindingExpression (target As DependencyObject, dp As DependencyProperty) As BindingExpression



dp 所在的绑定目标对象。The binding target object where dp is.


从中检索 BindingExpression 对象的绑定目标属性。The binding target property from which to retrieve the BindingExpression object.



与给定属性相关联的 BindingExpression 对象;如果不存在,则为 nullThe BindingExpression object associated with the given property or null if none exists. 如果在属性上设置了 PriorityBindingExpression 对象,则返回 ActiveBindingExpressionIf a PriorityBindingExpression object is set on the property, the ActiveBindingExpression is returned.


targetdp 参数不能为 nullThe target and dp parameters cannot be null.


下面的示例演示了一个 Click 事件处理程序的实现,该事件处理程序使用 GetBindingExpression 方法获取 BindingExpression ,然后调用 DataItem 属性来访问绑定源对象。The following example shows the implementation of a Click event handler that uses the GetBindingExpression method to obtain the BindingExpression and then calls the DataItem property to access the binding source object.

TextBlock SavingsText 是绑定目标对象, Text 是绑定目标属性。The TextBlockSavingsText is the binding target object and Text is the binding target property.

private void OnRentRaise(Object sender, RoutedEventArgs args)
  // Update bills
  System.Random random = new System.Random();
  double i = random.Next(10);
  BindingExpression bindingExpression =
    BindingOperations.GetBindingExpression(SavingsText, TextBlock.TextProperty);
  SDKSample.NetIncome sourceData = (SDKSample.NetIncome) bindingExpression.DataItem;
  sourceData.Rent = (int)((1 + i / 100) * (double)sourceData.Rent);
Private Sub OnRentRaise(ByVal sender As Object, ByVal args As RoutedEventArgs)
    Dim _random As New System.Random()
    Dim num1 As Double = _random.Next(10)
    Dim expression1 As BindingExpression = BindingOperations.GetBindingExpression(Me.SavingsText, TextBlock.TextProperty)
    Dim income1 As NetIncome = DirectCast(expression1.DataItem, NetIncome)
    income1.Rent = CInt(((1 + (num1 / 100)) * income1.Rent))
End Sub


BindingExpression对象维护绑定源和绑定目标之间的连接。The BindingExpression object maintains the connection between the binding source and the binding target. 可以 BindingExpression 通过调用此静态方法或 GetBindingExpression 对数据绑定或对象调用方法来获取对象 FrameworkElement FrameworkContentElementYou can obtain the BindingExpression object by calling this static method or by calling the GetBindingExpression method on a data-bound FrameworkElement or FrameworkContentElement object.