在WPF中使用枚举作为依赖属性

8

我试图在自定义控件中使用枚举类型作为依赖属性,但总是出错:

public enum PriceCategories
    {
        First = 1,
        Second = 2,
        Third = 3,
        Fourth = 4,
        Fifth = 5,
        Sixth = 6
    }
    public static readonly DependencyProperty PriceCatProperty =
DependencyProperty.Register("PriceCat", typeof(PriceCategories), typeof(CustControl), new PropertyMetadata(PriceCategories.First));
};

    public PriceCategories PriceCat  // here I get an error "Expected class, delegate, enum, interface or struct"
    {
        get { return (PriceCategories)GetValue(PriceCatProperty); }
        set { SetValue(PriceCatProperty, value); }
    }

请看,哪里有错误?

1个回答

11

你的DP没有在类的作用域内声明。看起来在DP声明后面多了一个闭合括号。

public enum PriceCategories
{
  // ...
}
public static readonly DependencyProperty PriceCatProperty =
  DependencyProperty.Register("PriceCat", typeof(PriceCategories),
  typeof(CustControl),  new PropertyMetadata(PriceCategories.First));
};  // <-- this is probably closing the containing class

哎呀,是这个括号。谢谢! - rem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接