【问题标题】:Using enum as a dependency property in WPF在 WPF 中使用枚举作为依赖属性
【发布时间】:2009-12-07 05:19:08
【问题描述】:

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

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

请看。哪里出错了?

【问题讨论】:

    标签: c# wpf enums custom-controls dependency-properties


    【解决方案1】:

    您的 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
    

    【讨论】:

    • 糟糕,是的,就是这个支架。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多