2013-03-16 52 views
2

我希望它有可能設置用戶控件的「自動」字符串的高度屬性。DependencyProperty double接受「自動」似乎工作,但拋出錯誤

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); 

這似乎是工作得很好,但我在輸出窗口得到這些錯誤:

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)' 

我該怎麼做這個正常嗎?

回答

3

這裏的FrameworkElement.Height屬性的實現:

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

LengthConverter處理自動轉換爲double.NaN。

所以,你需要的是

[TypeConverter(typeof (LengthConverter))] 
public double ContentHeight 
{ 
    get { return GetValue(ContentHeightProperty); } 
    set { SetValue(ContentHeightProperty, value); } 
} 
+0

謝謝,工作得很好; D – natli 2013-03-16 16:36:49

+0

謝謝先生,正是我需要知道的 – TheZenker 2013-04-30 14:00:53

+0

是的,總是那些好的屬性...... :-) – florien 2017-06-30 00:13:16

0

我知道這有剛巧適合使用的情況下,要求一個答案,但遺憾的是不問問題的情況下。在我的情況下,我需要一個這個函數,我需要DependencyProperty,因爲它是作爲嵌入在控件內部的所有元素的靜態屬性訪問的。 (就像「Canvas.Top」)

[TypeConverter(typeof(LengthConverter))] 
public static readonly DependencyProperty ChildrenPaddingProperty = 
    DependencyProperty.RegisterAttached(
     "ChildrenPadding", 
     typeof(double), 
     typeof(Layout), 
     new FrameworkPropertyMetadata(10d, FrameworkPropertyMetadataOptions.AffectsArrange) 
    ); 

[TypeConverter(typeof(LengthConverter))] 
public static double GetChildrenPadding(UIElement control) { 
    return (double)control.GetValue(ChildrenPaddingProperty); 
} 
[TypeConverter(typeof(LengthConverter))] 
public static void SetChildrenPadding(UIElement control, double yn) { 
    control.SetValue(ChildrenPaddingProperty, yn); 
} 

幸運的是,我編譯時只做了很少的調整。請注意屬性是在DP的所有3個屬性上設置的。

相關問題