/// <summary> /// Assigns the specified control's value to the specified property. /// </summary> /// <param name="property">The property to assign the value to.</param> /// <param name="control">The control to get the value from.</param> /// <param name="tile">The tile object on which to assign the value.</param> public static void SetPropertyValueFromControl(PropertyInfo property, UIElement control, ITile tile) { if (property.GetCustomAttribute <ShowInEditorAttribute>().IsReadOnly) { return; // skip trying to assign a value that is supposed to be readonly } switch (property.PropertyType) { case var stringProp when stringProp.IsAssignableFrom(typeof(string)): property.SetValue(tile, (control as TextBox).Text); break; case var intProp when intProp.IsAssignableFrom(typeof(int)): property.SetValue(tile, int.Parse((control as TextBox).Text)); break; case var boolProp when boolProp.IsAssignableFrom(typeof(bool)): property.SetValue(tile, (control as CheckBox).IsChecked ?? false); // try not to assign null to a value type break; case var colorProp when colorProp.IsAssignableFrom(typeof(Color)): property.SetValue(tile, Colorful.String((control as TextBox).Text)); break; default: throw new InvalidOperationException($"[EditorHelper] Property '{property.Name}' of type '{property.PropertyType.Name}' is not supported by {nameof(ShowInEditorAttribute)}"); } }
private static bool TryGetColorFromString(string hex, out Color color) { try { color = Colorful.String(hex); return(true); } catch { color = Colors.White; return(false); } }