BindingGroup.GetValue(Object, String) Método

Definición

Devuelve el valor propuesto para la propiedad y el elemento especificados.

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

Parámetros

item
Object

Objeto que contiene la propiedad especificada.

propertyName
String

Propiedad cuyo valor propuesto se va a obtener.

Devoluciones

Valor de propiedad propuesto.

Excepciones

No hay ningún enlace para el elemento y la propiedad especificados.

El valor de la propiedad especificada no está disponible, debido a un error de conversión o porque se produjo un error en una regla de validación anterior.

Ejemplos

El ejemplo siguiente forma parte de una aplicación que pide al usuario que escriba varios clientes y asigne un representante de ventas a cada cliente. La aplicación comprueba que el representante de ventas y el cliente pertenecen a la misma región. En el ejemplo se muestra el Validate método , que usa el GetValue(Object, String) método para obtener los valores especificados por el cliente.

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;
        }
        else
        {

            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
        Else

            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

Comentarios

Use este método en el ValidationRule.Validate método para obtener el valor que se va a confirmar en el origen. El tipo del valor devuelto depende de la fase en la ValidationRule que se produce. Por ejemplo, si un TextBox es datos enlazados a una propiedad de tipo entero y que ValidationRule llama GetValue(Object, String) a tiene su ValidationStep establecido RawProposedValueen , el método devuelve una cadena. ValidationRule Si el objeto tiene establecido en ConvertedProposedValueValidationStep , el método devuelve el tipo devuelto por el convertidor del enlace. En este ejemplo, GetValue(Object, String) normalmente devuelve un entero.

Se aplica a