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

これにはたまたま質問のケースに適合する答えがあることは知っていますが、残念ながら、質問のケースには適合しません。私の場合、この関数が必要で、コントロール内にネストされたすべての要素の静的プロパティとしてアクセスする必要があったため、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 に答える