Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DependencyProperty.IsValidValue Method

Determines whether the provided value is accepted for the type of property through basic type checking, and also potentially if it is within the allowed range of values for that type.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)
public bool IsValidValue(
	Object value
)

Parameters

value
Type: System.Object

The value to check.

Return Value

Type: System.Boolean
true if the value is acceptable and is of the correct type or a derived type; otherwise, false.

For a dependency property, an allowed range of values for that type can be specified through a ValidateValueCallback that is provided in the dependency property registration.

This method calls IsValidType internally. If the dependency property in question has no ValidateValueCallback,then calling this method is effectively equivalent to calling IsValidType. If the dependency property does have a ValidateValueCallback, and if IsValidType would have returned true, then the value returned will be as implemented in the callback.

A null value is a valid value for reference type dependency properties, or for a Nullable<T> dependency property, and would return true for these cases. In cases where the dependency property is neither a reference nor a Nullable<T> type, IsValidType will return false for a null value rather than raise an exception.

The following example uses IsValidValue as a check before calling SetValue on the dependency property.

void TrySetValueWithValidate(DependencyObject target, DependencyProperty dp, object providedValue)
{
  if (dp.IsValidValue(providedValue))
  {
    target.SetValue(dp, providedValue);
  }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.