public SettingsItem(SettingsItemType type, string key, string meaning = null, string description = null) { InitializeComponent(); Key = key; SettingsItemType = type; Meaning = meaning; Description = description; labTitle.Text = Meaning == null ? Key : Meaning; switch (SettingsItemType) { case SettingsItemType.String: SetMainControl(new TextBox()); break; case SettingsItemType.Select: SetMainControl(new ComboBox()); ((ComboBox)GetMainControl()).DropDownStyle = ComboBoxStyle.DropDownList; break; case SettingsItemType.Boolean: SetMainControl(CreateCheckBox()); break; case SettingsItemType.Integer: SetMainControl(CreateNumberTextBox()); break; case SettingsItemType.Double: SetMainControl(CreateDoubleNumberTextBox()); break; case SettingsItemType.Password: SetMainControl(new TextBox()); ((TextBox)GetMainControl()).PasswordChar = '*'; break; case SettingsItemType.Image: break; case SettingsItemType.File: break; case SettingsItemType.None: break; case SettingsItemType.StringList: SetMainControl(CreateItemControl_StringList()); break; case SettingsItemType.StringDictionary: SetMainControl(CreateItemControl_StringDictionary()); break; } }
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); } } }
public SettingsAttribute(string meaning, string description, SettingsItemType itemType = SettingsItemType.None) { Meaning = meaning; Description = description; SettingsItemType = itemType; }
public SettingsBinding(SettingsItemType type, string name, ImageSource imageSource) { Type = type; Name = name; Icon = imageSource; }
private static SettingsItem CreateSettingsItem(int row, string text, Grid grid, SettingsItemType type) { if (type == SettingsItemType.Digit) { return(new DigitSettingsItem(row, text, grid)); } else { return(new ColorSettingsItem(row, text, grid)); } }