BindingGroup.TryGetValue(Object, String, Object) Method

Definition

Attempts to get the proposed value for the specified property and item.

public bool TryGetValue (object item, string propertyName, out object value);

Parameters

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.

Returns

true if value is the proposed value for the specified property; otherwise, false.

Examples

The following example creates a custom ValidationRule named ValidateDateAndPrice. In the Validate method, the example uses the TryGetValue method and the Items property to get the values the user entered into the form. Then the example checks that if an item is over 100 dollars, it will be available for at least seven days. 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;
    }
}

Remarks

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.

Use this method in the ValidationRule.Validate method to get the value to be committed to the source. The type value depends on the stage at which the ValidationRule occurs. 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. If the ValidationRule has its ValidationStep set to ConvertedProposedValue, the type of value is whatever type that is returned by the binding's converter. In this example, value is usually an integer.

Applies to

Produit Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9