private ItemControl_StringDictionary CreateItemControl_StringDictionary() { ItemControl_StringDictionary sd = new ItemControl_StringDictionary(); sd.Height = 300; return(sd); }
public void SetData(object data) { if (data == null) { return; } this.data = data; flp.Controls.Clear(); foreach (PropertyInfo pi in data.GetType().GetProperties()) { // 过滤标记无效的属性 if (pi.IsDefined(typeof(InvalidSettingsAttribute), false)) { continue; } // 含义 string meaning = null; // 详细信息 string description = null; // 属性的数据 object value = pi.GetValue(data, null); // 属性标注的类型 SettingsItemType itemType = SettingsItemType.None; // 属性项基本信息 if (pi.IsDefined(typeof(SettingsAttribute), false)) { SettingsAttribute attribute = (SettingsAttribute)pi.GetCustomAttribute(typeof(SettingsAttribute)); meaning = attribute.Meaning; description = attribute.Description; itemType = attribute.SettingsItemType; } SettingsItem item = null; if (pi.PropertyType == typeof(bool)) { if (itemType == SettingsItemType.None) { itemType = SettingsItemType.Boolean; } item = new SettingsItem(itemType, pi.Name, meaning, description); ((CheckBox)item.GetMainControl()).Checked = (bool)value; } else if (pi.PropertyType == typeof(int)) { if (itemType == SettingsItemType.None) { itemType = SettingsItemType.Integer; } item = new SettingsItem(itemType, pi.Name, meaning, description); item.GetMainControl().LostFocus += (s, e) => { bool b = int.TryParse(item.GetMainControl().Text, out int x); if (!b) { item.Focus(); } }; if (value == null) { value = 0; } ((TextBox)item.GetMainControl()).Text = value.ToString(); } else if (pi.PropertyType == typeof(string)) { if (itemType == SettingsItemType.None) { itemType = SettingsItemType.String; } item = new SettingsItem(itemType, pi.Name, meaning, description); if (value == null) { value = ""; } ((TextBox)item.GetMainControl()).Text = value.ToString(); } else if (pi.PropertyType == typeof(List <string>)) { if (itemType == SettingsItemType.None) { itemType = SettingsItemType.StringList; } item = new SettingsItem(itemType, pi.Name, meaning, description); if (value == null) { value = new List <string>(); } if (itemType == SettingsItemType.StringList) { ItemControl_StringList ctrl = ((ItemControl_StringList)item.GetMainControl()); ctrl.AddItems((List <string>)value); ctrl.Height = 200; } else if (itemType == SettingsItemType.Select) { ComboBox ctrl = ((ComboBox)item.GetMainControl()); string[] arr = ((List <string>)value).ToArray(); ctrl.Items.AddRange(arr); ctrl.Text = arr[0]; } } else if (pi.PropertyType == typeof(List <KeyValueItem>)) { if (itemType == SettingsItemType.None) { itemType = SettingsItemType.StringDictionary; } item = new SettingsItem(itemType, pi.Name, meaning, description); if (value == null) { value = new List <KeyValueItem>(); } ItemControl_StringDictionary ctrl = ((ItemControl_StringDictionary)item.GetMainControl()); ctrl.SetData((List <KeyValueItem>)value); ctrl.Height = 200; } else { } if (item != null) { // 禁用标记锁定的属性 if (pi.IsDefined(typeof(LockSettingsAttribute), false)) { item.GetMainControl().Enabled = false; } item.Width = flp.Width - 10; item.OnShowDescription += Item_OnShowDescription; flp.Controls.Add(item); } } }