如果属性的类型是字符串,默认属性值是可以接受任意字符串的,如果希望提供字符串值候选列表,可以通过标注ComboPropertyAttribute 的并设置ValueList属性的方式实现。多个值用“|”分隔。
注意,标注ComboPropertyAttribute的属性类型必须是 string。
public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync { [ComboProperty(ValueList = "Student|Teacher|Worker")] public string MyProperty { get; set; } public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext) { return new ExecuteResult(); } public override CommandScope GetCommandScope() { return CommandScope.ExecutableInServer; } }
在设计器中效果如下:
如果需要更细致的控制,可以通过ComboPropertyAttribute的其他属性来控制。
- 值与显示值不同
- 设置ComboPropertyAttribute 的 DisplayList 属性。
代码:
public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync { [ComboProperty(ValueList = "Student|Teacher|Worker", DisplayList = "学生|教师|工人")] public string MyProperty { get; set; } public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext) { return new ExecuteResult(); } public override CommandScope GetCommandScope() { return CommandScope.ExecutableInServer; } }
- 效果:
- 其他说明:
此方法可以使用户在选择时选择中文选项,而单元格实际保存值为英文,方便程序处理。
ValueList和DisplayList通过数量和顺序匹配。
如果DisplayList数量超出ValueList数量,多出部分会被忽略;如果DisplayList数量少于ValueList数量,不足部分会使用ValueList对应的值。
- 设置ComboPropertyAttribute 的 DisplayList 属性。
- 允许用户使用列表以外的值
- 设置ComboPropertyAttribute 的 IsSelectOnly属性。
代码:
public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync { [ComboProperty(ValueList = "Student|Teacher|Worker", IsSelectOnly = false)] public string MyProperty { get; set; } public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext) { return new ExecuteResult(); } public override CommandScope GetCommandScope() { return CommandScope.ExecutableInServer; } }
- 效果:
- 注意:
IsSelectOnly 为 False 时,DisplayList 设置会被忽略;不填时 IsSelectOnly 属性的默认值为 True。
- 设置ComboPropertyAttribute 的 IsSelectOnly属性。