Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyProperty double that accepts "Auto" seems to work but throws errors

I want it to be possible to set a height property on a user control with the "Auto" string.

public object ContentHeight
{
    get { return GetValue(ContentHeightProperty); }
    set { SetValue(ContentHeightProperty, value); }
}

public static readonly DependencyProperty ContentHeightProperty =
    DependencyProperty.Register("ContentHeight", 
    typeof(object), typeof(UcDataTempl), 
    new PropertyMetadata(new object(), (o, args) => { }, 
    (o, value) =>
        {
            if (value.Equals("Auto"))
                return Double.NaN;

            return value;
        }), value => true);

It seems to be working just fine but I get these errors in the output window:

System.Windows.Data Error: 23 : Cannot convert 'System.Object' 
from type 'Object' to type 'System.Double' for 'en-US' culture 
with default conversions; consider using Converter property of Binding. 
NotSupportedException:'System.NotSupportedException: DoubleConverter cannot convert from System.Object.

   at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
   at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'

System.Windows.Data Error: 6 : 'ObjectSourceConverter' 
converter failed to convert value 'System.Object' (type 'Object'); 
fallback value will be used, if available. BindingExpression:Path=ContentHeight; 
DataItem='UcDataTempl' (Name=''); 
target element is 'Border' (Name=''); 
target property is 'Height' (type 'Double') 
NotSupportedException:'System.NotSupportedException: DoubleConverter cannot convert from System.Object.

   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.DefaultValueConverter.ConvertFrom(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture)
   at MS.Internal.Data.ObjectSourceConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'

How do I do this properly?

like image 542
natli Avatar asked Dec 05 '25 15:12

natli


1 Answers

Here's the implementation of the FrameworkElement.Height property:

[TypeConverter(typeof (LengthConverter))]
public double Height
{
  get
  {
    return (double) this.GetValue(FrameworkElement.HeightProperty);
  }
  set
  {
    this.SetValue(FrameworkElement.HeightProperty, (object) value);
  }
}

LengthConverter handles conversion of Auto to double.NaN.

So what you need is

[TypeConverter(typeof (LengthConverter))]
public double ContentHeight
{
    get { return GetValue(ContentHeightProperty); }
    set { SetValue(ContentHeightProperty, value); }
}
like image 126
Phil Avatar answered Dec 07 '25 05:12

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!