c# combobox 绑定枚举方式
建立一个类 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Reflection;
using System.ComponentModel; namespace WindowsFormsApplication1
{
public class EnumDescription
{
public static string GetEnumDesc(Enum e)
{
FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.
GetCustomAttributes(typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > )
{
return EnumAttributes[].Description;
}
return e.ToString();
}
}
}
页面代码 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Init();
Init1();
} public void Init()
{
comboBox1.DataSource = System.Enum.GetNames(typeof(ENUm_Type));
} /// <summary>
/// 反射邦定枚举
/// </summary>
private void Init1()
{
Array arrs = System.Enum.GetValues(typeof(ENUm_Type)); // 获取枚举的所有值
DataTable dt = new DataTable();
dt.Columns.Add("String", Type.GetType("System.String"));
dt.Columns.Add("Value", typeof(int));
foreach (var arr in arrs)
{
string strText = EnumDescription.GetEnumDesc((ENUm_Type)arr);
DataRow aRow = dt.NewRow();
aRow[] = strText;
aRow[] = (int)arr;
dt.Rows.Add(aRow);
} comboBox1.DataSource = dt;
comboBox1.DisplayMember = "String";
comboBox1.ValueMember = "Value";
} private void button1_Click(object sender, EventArgs e)
{
// 第一种实现方式
//ENUm_Type eT = ENUm_Type.tet_1;
//comboBox1.SelectedIndex = comboBox1.FindString(eT.ToString()); //string str = comboBox1.SelectedItem.ToString(); // 第二种实现方式
int a = comboBox1.SelectedIndex;
System.Diagnostics.Trace.WriteLine(comboBox1.SelectedItem);
DataRowView dr = (DataRowView)(comboBox1.SelectedItem);
ENUm_Type aE = (ENUm_Type)(dr.Row[]);
} public enum ENUm_Type
{
[Description("tet_1")]
tet_1 = ,
[Description("tet_2")]
tet_2 = ,
[Description("tet_3")]
tet_3 = ,
}
} }
c# combobox 绑定枚举方式的更多相关文章
- c# ComboBox绑定枚举
定义枚举 public enum UserLevel { Commmon = , Administrator, Developer } 方法一 private void Method1() { com ...
- ASP.NET Core 四种方式绑定枚举值
前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
- 整理:WPF中Xaml中绑定枚举的写法
原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...
- 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据
[源码下载] 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过实 ...
- ListBox和ComboBox绑定数据简单例子
1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //自定义了Person类(有Name,Age,Heigth等属性) List<Person> per ...
- DataTrigger 绑定枚举
在触发器中绑定枚举类型: <ControlTemplate.Triggers> <DataTrigger Binding="{Binding CheckStateEnum} ...
- c#中DropDownList控件绑定枚举数据
c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...
- Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...
随机推荐
- vuex 表单字段映射工具 vuex-map-fields
vuex在处理表单的时候显得很麻烦,要一个字段一个字段的去写set和get还有mutation,字段多的话带来的工作量将是非常巨大的.vuex-map-fields将能很好的解决这个问题. vuex- ...
- python调用phantomjs组件(windows和linux)
phantomjs在windows和linux系统,可以通selenium的webdriver直接调用,所以只要将phantomjs程序加载到python程序目录下. 示例代码如下所示: #建立Pha ...
- Atom打开大文件卡死的问题替代方案
无解,本身是网页的框架,所以直接换回ST或者Notepad++吧.
- log4j 2使用properties文件进行配置
网上不少文章给的都是用xml进行配置,也会提到无法使用properties文件对log4j进行配置,但那应该只是在他们写文章的时候才是如此,最新的2.8.2版本经过我试验后是可以做到的当然该文件最好放 ...
- Storyboards Tutorial 01
Storyboarding 是在ios 5时候引进入的一个非常出色的特性.节省了为app创建user interfaces的时间.
- linux调整缓存写入磁盘的时间,减少磁盘爆掉的可能性
缓存数据存入磁盘的最长时间,如果这段时间写不完,就会报异常停止写,这样缓存数据会不断积累,导致内存爆掉. echo 0 > /proc/sys/kernel/hung_task_timeout_ ...
- ylb:转换函数Cast,Convert 指定格式返回
ylbtech-SQL Server:SQL Server-转换函数Cast,Convert 指定格式返回 转换函数Cast,Convert 指定格式返回. ylb:转换函数Cast,Convert ...
- antialiasing
Aliasing发生的原因在于,采样频率过低, 这样就无法重建更接近原来texture的图像, Temporal aliasing在时域上 采的texture 次数太少 不能重建真实 是帧与帧之间,s ...
- Linux常用的几个vi小命令
输入跳转命令: 命令行前 Ctrl+A 命令行后 Ctrl+E VI命令中: 当前行 行首 "0" 当前行 行尾 "Shift+4" 当前文档首行首字符:& ...
- 【Salvation】——怪物角色动画&主角碰撞死亡动画
写在前面:这个动画功能同样也是使用JavaScript编写脚本,在Unity3D游戏引擎的环境中实现,在怪物的角色动画中,很多与人物相同,这里不再重复. 一.设计敌人 拖一个精英sprite到层次面板 ...