BindingGroup.TryGetValue(Object, String, Object) 方法


嘗試取得指定之屬性與項目的建議值。Attempts to get the proposed value for the specified property and item.

 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



包含指定之屬性的物件。The object that contains the specified property.


要取得其建議值的屬性。The property whose proposed value to get.


當這個方法傳回時,傳回值會包含代表建議之屬性值的物件。When this method returns, contains an object that represents the proposed property value. 這個參數會以未初始化的狀態傳遞。This parameter is passed uninitialized.


如果這個值就是指定之屬性的建議值,則為 true,否則為 falsetrue if value is the proposed value for the specified property; otherwise, false.


下列範例會建立名為ValidationRule ValidateDateAndPrice的自訂。The following example creates a custom ValidationRule named ValidateDateAndPrice. 在方法中,此範例會TryGetValue使用方法和Items屬性來取得使用者在表單中輸入的值。 ValidateIn the Validate method, the example uses the TryGetValue method and the Items property to get the values the user entered into the form. 然後,此範例會檢查項目是否超過100美元,其將至少可供使用七天。Then the example checks that if an item is over 100 dollars, it will be available for at least seven days. 這個範例是BindingGroup類別上較大範例的一部分This example is part of a larger example on the BindingGroup class

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


TryGetValuefalse如果沒有指定專案和屬性的系結,或者指定的屬性值無法使用,則會傳回,因為轉換錯誤或先前的驗證規則失敗。TryGetValue returns false if there is not a binding for the specified item and property or if the value of the specified property is not available, due to a conversion error or because an earlier validation rule failed.

ValidationRule.Validate方法中使用這個方法,以取得要認可至來源的值。Use this method in the ValidationRule.Validate method to get the value to be committed to the source. 類型value取決於發生的ValidationRule階段。The type value depends on the stage at which the ValidationRule occurs. 例如, TextBox如果是系結至整數類型之屬性的資料, ValidationStep value ValidationRule則為,如果呼叫TryGetValue的已將其設定為RawProposedValue,則為字串。For example, if a TextBox is data bound to a property of type integer, value is a string if the ValidationRule that calls TryGetValue has its ValidationStep set to RawProposedValue. 如果將設為ConvertedProposedValue,則的類型就是系結的轉換器所傳回的任何類型。value ValidationStep ValidationRuleIf the ValidationRule has its ValidationStep set to ConvertedProposedValue, the type of value is whatever type that is returned by the binding's converter. 在此範例中value ,通常是整數。In this example, value is usually an integer.