2

我正在尝试使用枚举作为依赖属性的类型,但任何默认设置似乎都失败了。

编译成功,但是当我尝试在 XAML 中使用该对象时,我得到一个“Specified Cast is not Valid”错误和我的对象上的蓝色下划线。

VS2012 使用 WinRT。

我究竟做错了什么?

这是我正在使用的代码:

public class WheelPanel : Panel
{
    public enum ItemAlignmentOptions
    {
        Left, Center, Right
    }

    /// <summary>
    /// Identifies the <see cref="ItemAlignment" /> dependency property.
    /// </summary>

    // This fails using null

    //public static readonly DependencyProperty ItemAlignmentProperty = DependencyProperty.Register(
    //    ItemAlignmentPropertyName,
    //    typeof(ItemAlignmentOptions),
    //    typeof(WheelPanel),
    //    new PropertyMetadata(null,new PropertyChangedCallback(ItemAlignmentChanged)));

    // This fails using a default value

    //public static readonly DependencyProperty ItemAlignmentProperty = DependencyProperty.Register(
    //    ItemAlignmentPropertyName,
    //    typeof(ItemAlignmentOptions),
    //    typeof(WheelPanel),
    //    new PropertyMetadata(ItemAlignmentOptions.Center,new PropertyChangedCallback(ItemAlignmentChanged)));

    // This works!
    public static readonly DependencyProperty ItemAlignmentProperty = DependencyProperty.Register(
        ItemAlignmentPropertyName,
        typeof(ItemAlignmentOptions),
        typeof(WheelPanel),
        new PropertyMetadata(new PropertyChangedCallback(ItemAlignmentChanged)));

    /// <summary>
    /// The <see cref="ItemAlignment" /> dependency property's name.
    /// </summary>
    public const string ItemAlignmentPropertyName = "ItemAlignment";

    /// <summary>
    /// Gets or sets the value of the <see cref="ItemAlignment" />
    /// property. This is a dependency property.
    /// </summary>
    public ItemAlignmentOptions ItemAlignment
    {
        get { return (ItemAlignmentOptions)GetValue(ItemAlignmentProperty); }
        set { SetValue(ItemAlignmentProperty, value); }
    }
4

1 回答 1

1

尝试使用“1”作为值。枚举的底层类型是 Int32 (默认情况下),也许它需要一个整数作为默认值。

于 2012-11-12T03:31:34.157 回答