BindingGroup.TryGetValue(Object, String, Object) 메서드

정의

지정된 속성 및 항목에 대해 제안된 값을 가져오려고 시도합니다.

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

매개 변수

item
Object

지정된 속성을 포함하는 개체입니다.

propertyName
String

제안된 값을 가져올 속성입니다.

value
Object

이 메서드의 반환 값에는 제안된 속성 값을 나타내는 개체가 포함됩니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

Boolean

값이 지정된 속성에 제안된 값이면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 사용자 지정 ValidationRule 라는 ValidateDateAndPrice합니다. 에 Validate 메서드를 사용 하 여 합니다 TryGetValue 메서드 및 Items 사용자가 양식에 입력 한 값을 가져올 속성입니다. 다음 예제에서는 100 달러 개 항목을 사용 하는 경우이 수 있는 최소 7 일 동안 확인 합니다. 이 예제는에서 더 큰 예제의 일부는 BindingGroup 클래스

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

설명

TryGetValue 반환 false 지정한 항목 및 속성에 대 한 바인딩이 없는 경우 지정된 된 속성의 값을 변환 오류로 인해 사용할 수 없는 경우 또는 이전 유효성 검사 규칙을 실패 합니다.

이 메서드를 사용 합니다 ValidationRule.Validate 메서드는 소스에 커밋할 수 값을 가져옵니다. 형식 value 하는 단계에 따라 달라 집니다는 ValidationRule 발생 합니다. 예를 들어 경우는 TextBox 정수 형식의 속성에 바인딩된 데이터가 value 문자열인 경우를 ValidationRule 호출 하는 TryGetValue 에 해당 ValidationStepRawProposedValue합니다. 경우는 ValidationRule 에 해당 ValidationStep 로 설정 ConvertedProposedValue, 형식의 value 바인딩의 변환기에서 반환 되는 모든 형식입니다. 이 예제에서는 value 는 일반적으로 정수입니다.

적용 대상