Aracılığıyla paylaş


BindingGroup.TryGetValue(Object, String, Object) Yöntem

Tanım

Belirtilen özellik ve öğe için önerilen değeri almaya çalışır.

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

Parametreler

item
Object

Belirtilen özelliği içeren nesne.

propertyName
String

Elde etmek için önerilen değeri olan özellik.

value
Object

Bu yöntem döndürdüğünde, önerilen özellik değerini temsil eden bir nesne içerir. Bu parametre başlatılmamış olarak geçirilir.

Döndürülenler

true değer, belirtilen özellik için önerilen değerse; aksi takdirde , false.

Örnekler

Aşağıdaki örnek adlı ValidateDateAndPriceözel ValidationRule bir oluşturur. yönteminde Validate örnek, kullanıcının forma girdiği değerleri almak için yöntemini ve Items özelliğini kullanırTryGetValue. Daha sonra örnek, bir öğenin 100 doların üzerindeyse en az yedi gün boyunca kullanılabilir olacağını denetler. Bu örnek, sınıfındaki BindingGroup daha büyük bir örneğin parçasıdır

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

Açıklamalar

TryGetValue , belirtilen öğe ve özellik için bağlama yoksa veya belirtilen özelliğin değeri kullanılabilir değilse, dönüştürme hatası nedeniyle veya önceki bir doğrulama kuralı başarısız olduğundan döndürür false .

Kaynağa işlenecek değeri almak için yönteminde bu yöntemi ValidationRule.Validate kullanın. Türü value , oluştuğu aşamaya ValidationRule bağlıdır. Örneğin, tamsayı TextBox türündeki bir özelliğe bağlı bir veri ise, value çağrısının TryGetValueValidationStep olarak ayarlanmış RawProposedValueolması durumunda ValidationRule bir dizedir. ValidationRule değerine ayarlanmışsa ValidationStepConvertedProposedValuetürüvalue, bağlamanın dönüştürücüsü tarafından döndürülen türdür. Bu örnekte genellikle value bir tamsayıdır.

Şunlara uygulanır