BindingGroup.GetValue(Object, String) Metodo

Definizione

Restituisce il valore proposto per la proprietà e l'elemento specificati.

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

Parametri

item
Object

Oggetto contenente la proprietà specificata.

propertyName
String

Proprietà di cui ottenere il valore proposto.

Valori restituiti

Valore della proprietà proposto.

Eccezioni

Non esiste un'associazione per l'elemento e la proprietà specificati.

Il valore della proprietà specificata non è disponibile a causa di un errore di conversione o perché una regola di convalida precedente non è riuscita.

Esempio

L'esempio seguente fa parte di un'applicazione che richiede all'utente di immettere più clienti e assegnare un rappresentante di vendita a ogni cliente. L'applicazione verifica che il rappresentante di vendita e il cliente appartengano alla stessa area. L'esempio mostra il Validate metodo , che usa il GetValue(Object, String) metodo per ottenere i valori immessi dal 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

Commenti

Utilizzare questo metodo nel ValidationRule.Validate metodo per ottenere il commit del valore nell'origine. Il tipo del valore restituito dipende dalla fase in cui si verifica l'oggetto ValidationRule . Ad esempio, se un TextBox oggetto è associato a una proprietà di tipo integer e l'oggetto ValidationRule che chiama GetValue(Object, String) è ValidationStep impostato su RawProposedValue, il metodo restituisce una stringa. Se è ValidationRuleValidationStep impostato su ConvertedProposedValue, il metodo restituisce qualsiasi tipo restituito dal convertitore dell'associazione. In questo esempio, in genere restituisce GetValue(Object, String) un numero intero.

Si applica a