簡單實現PropertyGrid編輯Dictionary功能
阿新 • • 發佈:2018-12-26
本Dictionary的Key和Value都是列舉型別,本例並沒實現新增和刪除功能,只有編輯功能。
(1)自制彈出窗體
四個控制元件,ListBox:lBox,ComboBox:cBox,Button:btnOk,還有個Label。
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; using System.Collections; namespace WindowsFormsApplication2 { public partial class FrmDictionaryEditor : Form { private IDictionary dic; private List<string> cBoxList; private List<string> lBoxList; private bool initFinished = false; public FrmDictionaryEditor(IDictionary dic) { InitializeComponent(); this.MinimumSize = this.Size; this.MaximumSize = this.Size; InitData(dic); } private void InitData(IDictionary dic) { this.dic = dic; this.lBoxList = new List<string>(); foreach (var item in dic.Keys) { lBoxList.Add(item.ToString() + "," + dic[item].ToString()); } this.lBox.DataSource = lBoxList; this.cBoxList = new List<string>(); foreach (var item in dic.Values) { Type t = item.GetType(); if (t.IsEnum) { Array array = t.GetEnumNames(); for (int i = 0; i < array.Length; i++) { cBoxList.Add(array.GetValue(i).ToString()); } this.cBox.DataSource = this.cBoxList; } break; } if (this.lBox.Items.Count > 0) { this.lBox.SelectedIndex = 0; this.initFinished = true; this.lBox_SelectedIndexChanged(null, null); } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (this.lBox.SelectedItem != null && this.initFinished) { for (int i = 0; i < this.cBox.Items.Count; i++) { if (this.cBox.Items[i].ToString() == this.lBox.SelectedItem.ToString().Split(',')[1]) { this.cBox.SelectedIndex = i; break; } } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { object lBoxItem = this.lBox.SelectedItem; object cBoxItem = this.cBox.SelectedItem; if (lBoxItem != null && cBoxItem != null && this.initFinished) { int lBoxSelectedIndex = this.lBox.SelectedIndex; int cBoxSelectedIndex = this.cBox.SelectedIndex; this.lBoxList[lBoxSelectedIndex] = lBoxItem.ToString().Split(',')[0] + "," + cBoxItem.ToString(); this.lBox.DataSource = null; this.lBox.DataSource = this.lBoxList; this.cBox.SelectedIndex = cBoxSelectedIndex; } } private void button1_Click(object sender, EventArgs e) { IDictionaryEnumerator de = dic.GetEnumerator(); de.MoveNext(); Type keyType = de.Key.GetType();//キーの列挙タイプ foreach (var item in this.lBoxList) { string strKey = item.Split(',')[0]; string strValue = item.Split(',')[1]; object enmKey = Enum.Parse(keyType, strKey); Type t = dic[enmKey].GetType(); if (t.IsEnum) { dic[enmKey] = Enum.Parse(t, strValue); } } this.DialogResult = DialogResult.Yes; this.Close(); } } }
(2)繼承UITypeEditor類,重寫EditValue跟GetEditStyle方法,將彈出視窗設為DictionaryUITypeEditorForm
public class DictionaryUITypeEditor : UITypeEditor { public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value) { FrmDictionaryEditor frm = new FrmDictionaryEditor(value as IDictionary); frm.ShowDialog(); return value; } public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } }
使用方法:
在Dictionary之前加上[Editor(typeof(DictionaryUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]即可,表示本Dictionary屬性使用的編輯器是DictionaryUITypeEditor
[Editor(typeof(DictionaryUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))] public Dictionary<EnmTaskType, EnmSkillLevel> Skills { get { return m_Skills; } set { m_Skills = value; } }
簡單例項:
AppSettings p = new AppSettings();
Dictionary<EnmTaskType, EnmSkillLevel> dic = new Dictionary<EnmTaskType, EnmSkillLevel>();
private void InitData()
{
dic.Add(EnmTaskType.DetailedDesign, EnmSkillLevel.High);
dic.Add(EnmTaskType.FormProgramming, EnmSkillLevel.Highest);
dic.Add(EnmTaskType.IntegrationTest, EnmSkillLevel.Low);
dic.Add(EnmTaskType.PreliminaryDesign, EnmSkillLevel.None);
dic.Add(EnmTaskType.ReportProgramming, EnmSkillLevel.Normal);
dic.Add(EnmTaskType.UnitTest, EnmSkillLevel.Highest);
}
public Form1()
{
InitializeComponent();
this.propertyGrid1.SelectedObject = p;
InitData();
}