public void SetProperty(XamlElement elem, IDictionary <String, PropertyInfo> props) { if (!props.TryGetValue(Property, out PropertyInfo prop)) { throw new XamlException($"The property '{Property}' not found in '{elem.GetType().Name}'"); } var valBind = GetBinding(nameof(Value)); if (valBind != null) { elem.SetBinding(Property, valBind); return; } if (Value == null) { return; } if (prop.PropertyType.IsEnum) { var converter = TypeDescriptor.GetConverter(prop.PropertyType); var enumVal = converter.ConvertFromString(Value.ToString()); prop.SetValue(elem, enumVal); } else { var propType = prop.PropertyType; if (propType.IsNullableType()) { propType = Nullable.GetUnderlyingType(propType); } var val = Convert.ChangeType(Value, propType); prop.SetValue(elem, val); } }
public void Set(XamlElement elem) { var props = elem.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var d = new Dictionary <String, PropertyInfo>(); foreach (var prop in props) { d.Add(prop.Name, prop); } foreach (var setter in this) { setter.SetProperty(elem, d); } }