Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Dependency propertie validation

Dependency propertie validation

Scheduled Pinned Locked Moved WPF
question
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    columbos14927
    wrote on last edited by
    #1

    Hello, Ive created a dependency property:

    class FrameworkElement : UIElement
    {
    public static readonly DependencyProperty NumberProperty;

        public FrameworkElement() { }
    
        static FrameworkElement()
        {
            FrameworkPropertyMetadata metadata =
                new FrameworkPropertyMetadata(Double.NaN,
                                              FrameworkPropertyMetadataOptions.AffectsMeasure);
    
            NumberProperty = DependencyProperty.Register("Number",//Propertie name
                                                          typeof(Double),//Data used by the prpertie
                                                          typeof(FrameworkElement),//The type that owns the propertie
                                                          metadata,//Meta data object
                                                          new ValidateValueCallback(FrameworkElement.IsNumberValid));//Validation callback
    
        }
    
        public Double Number
        {
            set { SetValue(NumberProperty, value); }
            get { return (Double)GetValue(NumberProperty); }
        }
    
    
        private static Boolean IsNumberValid(object value)
        {
            Double number = (Double)value;
    
            if ((number < 0) || (number > 100))
                return false;
            return true;
        }
    }
    

    and i added to it a validation callback. When i set an out of range number to that proprty i get an:ArgumentException. How can i do that instead of an exception the propertie will get some default value when i try to set out of range number? Thanks

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups