2

我希望可以使用“Auto”字符串在用户控件上设置高度属性。

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

我该如何正确地做到这一点?

4

2 回答 2

3

这是FrameworkElement.Height属性的实现:

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

LengthConverter处理 Auto 到 double.NaN 的转换。

所以你需要的是

[TypeConverter(typeof (LengthConverter))]
public double ContentHeight
{
    get { return GetValue(ContentHeightProperty); }
    set { SetValue(ContentHeightProperty, value); }
}
于 2013-03-16T12:14:23.090 回答
0

我知道这有一个答案恰好适合用于询问的情况,但遗憾的是不适合用于询问的情况。在我的情况下,我需要一个 this 函数并且我需要 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 个属性上设置的。

于 2015-11-16T04:13:56.283 回答