locked
Decimal DependencyProperty RRS feed

  • Question

  • I have defined a DependencyProperty with the following code:

    public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(Decimal), typeof(NumericValidator), new PropertyMetadata(Decimal.MinValue));
    public Decimal Minimum
    {
         get { return (Decimal)GetValue(MinimumProperty); }
         set { SetValue(MinimumProperty, value); }
    }

    When I try to set the value in xaml with the value of 1.0 I get an AG_E_PARSER_BAD_PROPERTY_VALUE.

    Any idea why this might be happening?

    Thursday, January 15, 2009 5:40 PM

Answers

  • I might be wrong, but what you are seeing is consistent with possibility that the Silverlight XAML parser doesn't have native FromString -style typeconversion support for Decimal even with the suffix and always assumes Double type with that input.

     You could make this work by applying your own TypeConverter, I am pretty sure:

    [TypeConverter(typeof(CustomDecimalConverter))]

    public Decimal Minimum

    {
         get { return (Decimal)GetValue(MinimumProperty); }
         set { SetValue(MinimumProperty, value); }
    }

    public CustomDecimalConverter : TypeConverter {

     public override Object ConvertFrom(
        ITypeDescriptorContext context,
        CultureInfo culture,
        Object value
     ) {    // convert a value of String into a return Object of type Decimal using System.Convert calls }

    // and implement CanConvertFrom to return true at least for String

    }

    It's too bad that Silverlight library doesn't include all the basic *Converter implementations from System.ComponentModel that full .NET has because then you could have just TypeConverterAttribute'd DecimalConverter rather than having to write this very basic typeconverter.

    Wednesday, January 21, 2009 9:19 PM

All replies

  •  It appears that if I use Double instead of Decimal I don't have the problem.

    Thursday, January 15, 2009 5:44 PM
  • If you need to use Decimal, you could explicitly declare your value as a decimal, use the M suffix:

    decimal d = 3.0M;

     

    Friday, January 16, 2009 12:41 AM
  • That works in c# but not in xaml.

    Tuesday, January 20, 2009 3:08 PM
  • I might be wrong, but what you are seeing is consistent with possibility that the Silverlight XAML parser doesn't have native FromString -style typeconversion support for Decimal even with the suffix and always assumes Double type with that input.

     You could make this work by applying your own TypeConverter, I am pretty sure:

    [TypeConverter(typeof(CustomDecimalConverter))]

    public Decimal Minimum

    {
         get { return (Decimal)GetValue(MinimumProperty); }
         set { SetValue(MinimumProperty, value); }
    }

    public CustomDecimalConverter : TypeConverter {

     public override Object ConvertFrom(
        ITypeDescriptorContext context,
        CultureInfo culture,
        Object value
     ) {    // convert a value of String into a return Object of type Decimal using System.Convert calls }

    // and implement CanConvertFrom to return true at least for String

    }

    It's too bad that Silverlight library doesn't include all the basic *Converter implementations from System.ComponentModel that full .NET has because then you could have just TypeConverterAttribute'd DecimalConverter rather than having to write this very basic typeconverter.

    Wednesday, January 21, 2009 9:19 PM
  • This seems to be the case in Silverlight 4 still!

    Any idea if MS has plans to fix this?

    Monday, February 21, 2011 11:54 PM
  • Still the same in Silverlight 5. Yell

    Tuesday, March 13, 2012 7:47 PM