PropertyGrid中的枚举 - 简单自定义排序顺序

时间:2009-03-25 17:14:17

标签: c#

我有一个枚举,我想自定义它在属性网格下拉列表中显示的顺序。

public enum Dir : int
{
   None = 0,
   North = (1 << 1),
   South = (1 << 2),
   East = (1 << 3),
   West = (1 << 4),
   NorthWest = North | West,
   NorthEast = North | East,
   SouthWest = South | West,
   SouthEast = South | East
} 

我希望下拉列表的显示顺序与我声明的枚举顺序相同,但是它按数字顺序排序。

我知道可以使用类型转换器,但是可以不用吗?

1 个答案:

答案 0 :(得分:2)

这些值是否已修复?我假设您正在使用类似伪标志的枚举,以便您了解该值是否“完全像北方”。如果是这种情况(这有点奇怪 - 这意味着你可以拥有South | North,但没关系)如果你设置一个额外的位也会有关系吗?

public enum Dir : int
{
   None = 0,
   North = (1 << 1),
   South = (1 << 2),
   East = (1 << 3),
   West = (1 << 4),
   NorthWest = (1 << 5) | North | West,
   NorthEast = (1 << 6) | North | East,
   SouthWest = (1 << 6) | South | West,
   SouthEast = (1 << 7) | South | East
}

这将保持数字顺序。 (如果你可以让West在East之前出现,或者NorthEast在NorthWest之前出现,那么会更容易也更一致。然后你可以使用(1 << 5)作为所有这些的额外位。