- Timestamp:
- 01/17/22 13:25:15 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Data.Views/3.3/EnumValueView.cs
r17180 r18201 85 85 } else { 86 86 valueComboBox.SelectedItem = Content.Value; 87 var value = (Enum)(object)Content.Value; 88 89 var zeroValue = Enum.ToObject(value.GetType(), Convert.ChangeType(0, value.GetTypeCode())); 90 var hasZeroValue = Enum.IsDefined(value.GetType(), zeroValue); 91 87 92 foreach (ListViewItem item in flagsListView.Items) { 88 93 var flag = (Enum)item.Tag; 89 item.Checked = ((Enum)(object)Content.Value).HasFlag(flag); 94 if (hasZeroValue && flag.Equals(zeroValue)) { // only show the zero-value if the whole value is zero, otherwise the zero-value would always be checked 95 item.Checked = value.Equals(zeroValue); 96 } else { 97 item.Checked = value.HasFlag(flag); 98 } 90 99 } 91 100 } … … 108 117 else { 109 118 valueComboBox.SelectedItem = Content.Value; 119 var value = (Enum)(object)Content.Value; 120 121 var zeroValue = value.GetZeroValue<T>(); 122 var hasZeroValue = Enum.IsDefined(value.GetType(), zeroValue); 123 110 124 foreach (ListViewItem item in flagsListView.Items) { 111 125 var flag = (Enum)item.Tag; 112 item.Checked = ((Enum)(object)Content.Value).HasFlag(flag); 126 if (hasZeroValue && flag.Equals(zeroValue)) { // only show the zero-value if the whole value is zero, otherwise the zero-value would always be checked 127 item.Checked = value.Equals(zeroValue); 128 } else { 129 item.Checked = value.HasFlag(flag); 130 } 113 131 } 114 132 } … … 116 134 117 135 private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) { 118 if ( (Content != null)&& !Content.ReadOnly)136 if (Content != null && !Content.ReadOnly) 119 137 Content.Value = (T)valueComboBox.SelectedItem; 120 138 } … … 122 140 private void flagsListView_ItemChecked(object sender, ItemCheckedEventArgs e) { 123 141 var flag = (T)e.Item.Tag; 124 if ((Content != null) && !Content.ReadOnly) 125 Content.Value = ((Enum)(object)Content.Value).SetFlag(flag, e.Item.Checked); 142 if (Content != null && !Content.ReadOnly) { 143 var value = (Enum)(object)Content.Value; 144 145 var zeroValue = value.GetZeroValue<T>(); 146 var hasZeroValue = Enum.IsDefined(value.GetType(), zeroValue); 147 148 if (hasZeroValue && flag.Equals(zeroValue)) { 149 if (e.Item.Checked) { // Checking zero-value => set zero-value 150 Content.Value = value.GetZeroValue<T>(); 151 } else if (value.Equals(zeroValue)) { // Un-checking zero-value => set all-value 152 Content.Value = value.GetAllValue<T>(); 153 } 154 } else { 155 Content.Value = value.SetFlag(flag, e.Item.Checked); 156 } 157 } 126 158 } 127 159 } … … 139 171 return (T)valueAsBase; 140 172 } 173 174 public static T GetZeroValue<T>(this Enum value) { 175 return (T)Enum.ToObject(value.GetType(), Convert.ChangeType(0, value.GetTypeCode())); 176 } 177 178 public static T GetAllValue<T>(this Enum value) { 179 var allValues = (Enum)(object)value.GetZeroValue<T>(); 180 foreach (T fl in Enum.GetValues(value.GetType())) 181 allValues = (Enum)(object)allValues.SetFlag(fl, true); 182 return (T)(object)allValues; 183 } 141 184 } 142 185 }
Note: See TracChangeset
for help on using the changeset viewer.