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

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

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

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/

相关文章:

java - 如何使用 JMockit 模拟/伪造一个新的枚举值

swift - 如何在 Swift 中为私有(private)枚举编写一个 equal 方法

c# - 如何从bot framework C#中读取附件内容?

c# - Linq to Entities Left outer join分组到一个集合中

c# - WPF 标记扩展如何引发编译错误?

c# - 使用 WPF 密码绑定(bind)在类型中找不到可附加属性

C# HttpWebRequest 日期 header 格式化

c# - 我可以使用反射来获取已被 new 关键字覆盖的基本属性吗?

c# - WPF 简单绑定(bind)

c# - 显示枚举中的 int 值