Using enum as a dependency property in WPF

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

I try to use enum type as a dependency property in my custom control, but always get an error:

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

Please, look. Where is mistake?

回答1:

Your DP is not being declared within the scope of a class. It looks like you have an extra closing brace after the DP declaration.

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!