BindingGroup.TryGetValue(Object, String, Object) Metoda

Definicja

Próbuje uzyskać proponowaną wartość dla określonej właściwości i elementu.

public:
 bool TryGetValue(System::Object ^ item, System::String ^ propertyName, [Runtime::InteropServices::Out] System::Object ^ % value);
public bool TryGetValue (object item, string propertyName, out object value);
member this.TryGetValue : obj * string * obj -> bool
Public Function TryGetValue (item As Object, propertyName As String, ByRef value As Object) As Boolean

Parametry

item
Object

Obiekt, który zawiera określoną właściwość.

propertyName
String

Właściwość, której proponowana wartość ma być pobrana.

value
Object

Gdy ta metoda zwraca wartość, zawiera obiekt reprezentujący proponowaną wartość właściwości. Ten parametr jest przekazywany jako niezainicjowany.

Zwraca

true jeśli wartość jest proponowaną wartością określonej właściwości; w przeciwnym razie , false.

Przykłady

W poniższym przykładzie zostanie utworzony niestandardowy element ValidationRule o nazwie ValidateDateAndPrice. W metodzie w przykładzie Validate użyto TryGetValue metody i Items właściwości , aby uzyskać wartości wprowadzone przez użytkownika w formularzu. Następnie przykład sprawdza, czy jeśli element wynosi ponad 100 dolarów, będzie dostępny przez co najmniej siedem dni. Ten przykład jest częścią większego przykładu w BindingGroup klasie

public class ValidateDateAndPrice : ValidationRule
{
    // Ensure that an item over $100 is available for at least 7 days.
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        BindingGroup bg = value as BindingGroup;

        // Get the source object.
        PurchaseItem item = bg.Items[0] as PurchaseItem;
        
        object doubleValue;
        object dateTimeValue;

        // Get the proposed values for Price and OfferExpires.
        bool priceResult = bg.TryGetValue(item, "Price", out doubleValue);
        bool dateResult = bg.TryGetValue(item, "OfferExpires", out dateTimeValue);

        if (!priceResult || !dateResult)
        {
            return new ValidationResult(false, "Properties not found");
        }

        double price = (double)doubleValue;
        DateTime offerExpires = (DateTime)dateTimeValue;

        // Check that an item over $100 is available for at least 7 days.
        if (price > 100)
        {
            if (offerExpires < DateTime.Today + new TimeSpan(7, 0, 0, 0))
            {
                return new ValidationResult(false, "Items over $100 must be available for at least 7 days.");
            }
        }

        return ValidationResult.ValidResult;
    }
}
Public Class ValidateDateAndPrice
    Inherits ValidationRule
    ' Ensure that an item over $100 is available for at least 7 days.
    Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As CultureInfo) As ValidationResult
        Dim bg As BindingGroup = TryCast(value, BindingGroup)

        ' Get the source object.
        Dim item As PurchaseItem = TryCast(bg.Items(0), PurchaseItem)

        Dim doubleValue As Object = Nothing
        Dim dateTimeValue As Object = Nothing

        ' Get the proposed values for Price and OfferExpires.
        Dim priceResult As Boolean = bg.TryGetValue(item, "Price", doubleValue)
        Dim dateResult As Boolean = bg.TryGetValue(item, "OfferExpires", dateTimeValue)

        If (Not priceResult) OrElse (Not dateResult) Then
            Return New ValidationResult(False, "Properties not found")
        End If

        Dim price As Double = CDbl(doubleValue)
        Dim offerExpires As Date = CDate(dateTimeValue)

        ' Check that an item over $100 is available for at least 7 days.
        If price > 100 Then
            If offerExpires < Date.Today + New TimeSpan(7, 0, 0, 0) Then
                Return New ValidationResult(False, "Items over $100 must be available for at least 7 days.")
            End If
        End If

        Return ValidationResult.ValidResult

    End Function
End Class

Uwagi

TryGetValue Zwraca false wartość , jeśli nie istnieje powiązanie dla określonego elementu i właściwości lub wartość określonej właściwości jest niedostępna z powodu błędu konwersji lub z powodu niepowodzenia wcześniejszej reguły walidacji.

Użyj tej metody w metodzie ValidationRule.Validate , aby uzyskać wartość, która ma zostać zatwierdzona do źródła. Typ value zależy od etapu ValidationRule , w którym występuje. Jeśli na przykład element TextBox jest danymi powiązanymi z właściwością typu liczba całkowita, jest ciągiem, value jeśli ValidationRule obiekt TryGetValue wywołujący ma ustawioną RawProposedValuewartość ValidationStep . Jeśli właściwość ValidationRule ma ustawioną ValidationStep wartość ConvertedProposedValue, typ value to dowolny typ zwracany przez konwerter powiązania. W tym przykładzie value jest to zwykle liczba całkowita.

Dotyczy