BindingOperations.GetBindingExpression 方法

定义

返回与指定对象上的指定的绑定目标关联的 BindingExpression 对象。

public:
 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

参数

target
DependencyObject

dp所在的绑定目标对象。

dp
DependencyProperty

从中检索 BindingExpression 对象的绑定目标属性。

返回

与给定属性相关联的 BindingExpression 对象;如果不存在,则为 null。 如果在属性上设置了 PriorityBindingExpression 对象,则返回 ActiveBindingExpression

例外

targetdp 参数不能为 null

示例

下面的示例演示事件处理程序的 Click 实现,该事件处理程序使用 GetBindingExpression 方法获取 BindingExpression ,然后调用 DataItem 属性以访问绑定源对象。

TextBlockSavingsText是绑定目标对象,Text是绑定目标属性。

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 维护绑定源和绑定目标之间的连接。 可以通过调用此静态方法或通过对数据绑定FrameworkElementFrameworkContentElement 对象调用 GetBindingExpression 方法来获取 BindingExpression 对象。

适用于

另请参阅