我尝试将枚举类型用作自定义控件中的依赖项属性,但始终会收到错误消息:

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); }
    }

敬请期待。错误在哪里?

最佳答案

没有在类范围内声明您的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

关于c# - 在WPF中将枚举用作依赖项属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1857967/

10-11 02:05