我正在尝试使用枚举作为依赖属性的类型,但任何默认设置似乎都失败了。
编译成功,但是当我尝试在 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); }
}