Search code examples
silverlightdependency-propertiesunsigned

In Silverlight, can one define min/max values for a DependencyProperty?


For example, I have a dependency property that changes the ScaleTransform of a Canvas, but if it ever goes below zero it throws an error. Sure, I could just force it to zero in the code if that ever happens, but I'd rather use a better method like using a udouble (unsigned double), which doesn't exist in Silverlight or even setting the min/max values somewhere in the DependencyProperty.

What's the best approach here?


Solution

  • If you're going to handle this in your DependencyProperty, I'd recommend handling it in a PropertyChangedCallback, which validates that the value is in the correct range and overrides it if not.

    You could also handle this outside of the dependency property. For instance: