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

示例

The following example creates a custom ValidationRule named 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

注解

TryGetValuefalse如果指定的项和属性没有绑定,或者由于转换错误或早期验证规则失败,指定属性的值不可用,则返回。

在方法中使用 ValidationRule.Validate 此方法可获取要提交到源的值。 value类型取决于发生的阶段ValidationRule。 例如,如果 TextBox 数据绑定到类型为整数的属性, value 则为字符串(如果该 ValidationRule 调用 TryGetValueValidationStep 设置为 RawProposedValue)。 ValidationRule如果已将其ValidationStep设置为ConvertedProposedValue,则其类型value是绑定转换器返回的任何类型。 在此示例中, value 通常是整数。

适用于