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

定义

尝试获取指定属性和项的建议值。Attempts to get the proposed value for the specified property and item.

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

包含指定属性的对象。The object that contains the specified property.

propertyName
String

要获取其建议值的属性。The property whose proposed value to get.

value
Object

此方法在返回时包含一个表示建议的属性值的对象。When this method returns, contains an object that represents the proposed property value. 此参数未经初始化即被传递。This parameter is passed uninitialized.

返回

Boolean

如果值为指定属性的建议值,则为 true;否则为 falsetrue if value is the proposed value for the specified property; otherwise, false.

示例

下面的示例创建一个名为的自定义 ValidationRule ValidateDateAndPriceThe following example creates a custom ValidationRule named ValidateDateAndPrice. Validate 方法中,该示例使用 TryGetValue 方法和 Items 属性来获取用户在窗体中输入的值。In the Validate method, the example uses the TryGetValue method and the Items property to get the values the user entered into the form. 然后,该示例将检查某一项是否超过100美元,该项目将在至少7天内可用。Then the example checks that if an item is over 100 dollars, it will be available for at least seven days. 此示例是类中更大示例的一部分 BindingGroupThis 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 取决于发生的阶段 ValidationRuleThe type value depends on the stage at which the ValidationRule occurs. 例如,如果 TextBox 将绑定到类型为 integer 的属性的数据, value 则如果 ValidationRule 调用的 TryGetValue ValidationStep 设置为,则为字符串 RawProposedValueFor 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. 如果将 ValidationRuleValidationStep 设置为,则的 ConvertedProposedValue 类型 value 是绑定的转换器返回的任何类型。If 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.

适用于