BindingGroup.GetValue(Object, String) 方法


返回指定的属性和项的建议值。Returns the proposed value for the specified property and item.

 System::Object ^ GetValue(System::Object ^ item, System::String ^ propertyName);
public object GetValue (object item, string propertyName);
override this.GetValue : obj * string -> obj
Public Function GetValue (item As Object, propertyName As String) As Object



包含指定属性的对象。The object that contains the specified property.


要获取其建议值的属性。The property whose proposed value to get.



建议的属性值。The proposed property value.


指定项和属性之间没有绑定。There is not a binding for the specified item and property.

由于转换错误或早期验证规则失败,指定的属性值不可用。The value of the specified property is not available, due to a conversion error or because an earlier validation rule failed.


下面的示例是一个应用程序的一部分,该应用程序提示用户输入多个客户,并为每个客户分配一个销售代表。The following example is part of an application that prompts the user to enter multiple customers and assign a sales representative to each customer. 应用程序将检查销售代表和客户是否属于同一区域。The application checks that the sales representative and the customer belong to the same region. 该示例演示 Validate 方法,该方法使用 GetValue(Object, String) 方法获取客户输入的值。The example shows the Validate method, which uses the GetValue(Object, String) method to get values that the customer entered.

public class AreasMatch : ValidationRule
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        BindingGroup bg = value as BindingGroup;
        Customer cust = bg.Items[0] as Customer;
        if (cust == null)
            return new ValidationResult(false, "Customer is not the source object");

        Region region = (Region)bg.GetValue(cust, "Location");
        ServiceRep rep = bg.GetValue(cust, "ServiceRepresentative") as ServiceRep;
        string customerName = bg.GetValue(cust, "Name") as string;

        if (region == rep.Area)
            return ValidationResult.ValidResult;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} must be assigned a sales representative that serves the {1} region. \n ", customerName, region);
            return new ValidationResult(false, sb.ToString());
Public Class AreasMatch
    Inherits ValidationRule
    Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As ValidationResult
        Dim bg As BindingGroup = TryCast(value, BindingGroup)
        Dim cust As Customer = TryCast(bg.Items(0), Customer)

        If cust Is Nothing Then
            Return New ValidationResult(False, "Customer is not the source object")
        End If

        Dim region As Region = CType(bg.GetValue(cust, "Location"), Region)
        Dim rep As ServiceRep = TryCast(bg.GetValue(cust, "ServiceRepresentative"), ServiceRep)
        Dim customerName As String = TryCast(bg.GetValue(cust, "Name"), String)

        If region = rep.Area Then
            Return ValidationResult.ValidResult

            Dim sb As New StringBuilder()
            sb.AppendFormat("{0} must be assigned a sales representative that serves the {1} region. " & vbLf & " ", customerName, region)
            Return New ValidationResult(False, sb.ToString())
        End If
    End Function
End Class


在方法中使用此方法 ValidationRule.Validate 可获取要提交到源的值。Use this method in the ValidationRule.Validate method to get the value to be committed to the source. 返回值的类型取决于发生的阶段 ValidationRuleThe type of the return value depends on the stage at which the ValidationRule occurs. 例如,如果 TextBox 将数据绑定到类型为 integer 的属性,并且调用的 ValidationRule GetValue(Object, String) ValidationStep 将其设置为,则 RawProposedValue 该方法将返回一个字符串。For example, if a TextBox is data bound to a property of type integer and the ValidationRule that calls GetValue(Object, String) has its ValidationStep set to RawProposedValue, the method returns a string. 如果将 ValidationRuleValidationStep 设置为 ConvertedProposedValue ,则方法将返回绑定的转换器返回的任何类型。If the ValidationRule has its ValidationStep set to ConvertedProposedValue, the method returns whatever type that is returned by the binding's converter. 在此示例中, GetValue(Object, String) 通常返回整数。In this example, GetValue(Object, String) usually returns an integer.