我希望它有可能設置用戶控件的「自動」字符串的高度屬性。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)'
我該怎麼做這個正常嗎?
謝謝,工作得很好; D – natli 2013-03-16 16:36:49
謝謝先生,正是我需要知道的 – TheZenker 2013-04-30 14:00:53
是的,總是那些好的屬性...... :-) – florien 2017-06-30 00:13:16