This documentation is archived and is not being maintained.

BindingGroup.TryGetValue Method

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

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)

'Declaration
Public Function TryGetValue ( _
	item As Object, _
	propertyName As String, _
	<OutAttribute> ByRef value As Object _
) As Boolean

Parameters

item
Type: System.Object
The object that contains the specified property.
propertyName
Type: System.String
The property whose proposed value to get.
value
Type: System.Object%
When this method returns, contains an object that represents the proposed property value. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if value is the proposed value for the specified property; otherwise, false.

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.

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
		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


.NET Framework

Supported in: 4, 3.5 SP1, 3.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: