3

consider the following scenario:

<TextBox Text="{Binding Price}"/>

here is the Price property

public Decimal Price
    {
        get
        {
            return _Price;
        }
        set
        {
            if (_Price != value)
            {
                _Price = value;
                OnPropertyChanged("Price");
            }
        }
    }

this method examines my property

private string validateGlassPrice()  
  {    
    if (GlassPrice <= 0)   
     {       
     return "price can't be 0 nor a minus value "; 
     } 
   else 
     {           
     return String.Empty;    
     }  
  }

this method examines my property if it was 0 or less - a minus value - now I need to examine if it is null or empty too, the problem is that Decimal won't accept nullable values, any workarounds ?

thanks in advance

Musaab
  • 805
  • 2
  • 13
  • 30

3 Answers3

3

You could use a Nullable Type

Alternatively, if it is undesirable to change your model, then bind to a property on your view model instead.

Typically, I prefer using string values in the view model for double/decimal values, and putting the validation in the view model property's setter. If an invalid decimal is passed, don't update the backing field.

At an appropriate point, copy the valid view model's property value to your model, for example just before the model needs to be persisted etc, or when the screen closes.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • thanks for your answer , but changing Decimal to any other type will cause me alot of rework , it is Decimal because my Entity-Framework decided so , any workaround would save me much time , which I really need – Musaab Jun 08 '11 at 12:44
  • Why do you need to validate against a null value? Are you binding the Price property directly to a TextBox? – devdigital Jun 08 '11 at 12:52
  • yeah I'm binding binding directly to a Textbox ,please see my XAML above – Musaab Jun 08 '11 at 13:03
1

Instead of using a validation function, I would use DataAnnotations. You can create custom ValidationAttributes. Also take a look at IDataErrorInfo in this Stackoverflow post. You can event handle the input's error in you action to disable them like with ICommand framework.

Community
  • 1
  • 1
Philippe Lavoie
  • 2,583
  • 5
  • 25
  • 39
  • I'm using the IDataErrorInfo , everything is fine in other Windows where there is no Decimal values , I have to stick with this type of implementation because I don't want to rework the already-done and working-fine WPF windows – Musaab Jun 08 '11 at 13:41
  • I'm just saying that, but if you don't want to rewrite stuff there is not much answer you can implement. Either mine or devdigital's solutions are fine but you must do a minimum of modification to make them work. I understand you don't want an invasive solution, but there often no easy way to do so. – Philippe Lavoie Jun 08 '11 at 13:50
0

I gone with devdigital updated solution , another String property

public String PriceString
    {
        get
        {
            return _PriceString;
        }
        set
        {
            Decimal result;




            if (Decimal.TryParse(value, out result))
            {
                _PriceString = value;
                GlassPrice = result;

            }
            else
            {
                GlassPrice = -33322;
                _PriceString = value;
            }
            OnPropertyChanged("PriceString");
        }
    }

I set GlassPrice to be -33322 when the textBox is actually blank, I just use this to differntiate null values from zero or minus values. surly except for -33322 itself.

Musaab
  • 805
  • 2
  • 13
  • 30
  • Don't use magic numbers. Use the nullable `decimal?` type. That's what it's for. Make `GlassPrice` a `decimal?`, and set it to `null` if the value entered is invalid. – Robert Rossney Jun 09 '11 at 08:20
  • well , because I don't want to re-edit the auto-generated - by my Entity-Framework - DAL classes , I have to keep GlassPrice Decimal – Musaab Jun 09 '11 at 14:25