阿尔瓦亚隆,省钱通怎么用,谢云汉
wpf中下拉框将键值对作为其数据源的具体操作。本实例以枚举类型以及枚举特性描述字符串生成键值对来进行。
namespace viewc
{
/// <summary>
/// view.xaml 的交互逻辑
/// </summary>
public partial class view : window
{
private enumtype_enumtype= enumtype.b;
public view()
{
initializecomponent();
initialcombox();
}
private void initialcombox()
{
dictionary<enumtype, string> keyvalues = new dictionary<enumtype, string>();
var pro = typeof(enumtype).getfields();//字段值
for (int i = 0; i < pro.count(); i++)
{
if (pro[i].fieldtype.isenum)//枚举类型
{
var descrips = (descriptionattribute[])pro[i].getcustomattributes(typeof(descriptionattribute), false);//特性描述
if (descrips.length < 0) continue;
var key = (enumtype)typeof(enumtype).invokemember(pro[i].name, system.reflection.bindingflags.getfield, null, null, null);//根据枚举名称得到相应枚举值
keyvalues.add(key, descrips[0].description);
}
}
cmbcontroltype.itemssource = keyvalues;
cmbcontroltype.displaymemberpath = "value";
cmbcontroltype.selectedvaluepath = "key";
}
private void window_loaded(object sender, routedeventargs e)
{
//this.cmbcontroltype.selectedvalue = _controlsteptype;//直接赋值selectvalue属性不会触发selectchanged事件
this.cmbcontroltype.selectedindex = 0;
}
private void cmbcontroltype_selectionchanged(object sender, selectionchangedeventargs e)
{
var controltype = (enumtype)this.cmbcontroltype.selectedvalue;
//dosomething
messagebox.show(controltype .tostring());
}
}
public enum enumtype
{
/// <summary>
/// aa
/// </summary>
[description("aa")]
a,
/// <summary>
/// bb
/// </summary>
[description("bb")]
b,
/// <summary>
/// cc
/// </summary>
[description("cc")]
c,
}
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论