我在我的所有UserControls中创建了一个枚举,其中包含可用于此UC上显示的此对象的操作。例如:
private enum ControlActions
{
New,
Edit,
Delete
}
几乎在每个UserControl中,操作都是相同的,所以我想创建一个全局枚举。但问题是,有些UserControls没有该对象的所有这些操作(有时只有一个,有时只有两个)。所以我的问题是,有没有一种方法可以创建一个全局枚举并对其进行分隔?
例如:在UserControl1中新建和编辑操作UserControl2中的删除操作UserControl3中的新建、编辑和删除操作
发布于 2019-06-26 14:45:12
您可能无法在枚举本身中解决此问题,但您可以编写一个扩展方法来确定某个控件是否允许该操作。
public enum ControlActions
{
New,
Edit,
Delete
}
public static class ControlActionsExtender
{
public static bool IsActionAllowed(this ControlActions controlAction, Type controlType)
{
switch (controlAction)
{
case ControlActions.New:
return controlType == typeof(UserControlA) || controlType == typeof(UserControlB) || controlType == typeof(UserControlC);
case ControlActions.Edit:
return controlType == typeof(UserControlA);
case ControlActions.Delete:
return controlType == typeof(UserControlA) || controlType == typeof(UserControlC);
}
return false;
}
}
然后,在用户控件中,可以通过调用以下命令来确定是否允许某个操作:
// See if we are allowed to create a new item from this control
ControlActions.New.IsAllowed(this.GetType());
https://stackoverflow.com/questions/56775017
复制