private static void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (sender is TextBlock element && e.NewValue != null) { string propName = (string)element.GetValue(TargetProperty); PropertyInfo prop = element.DataContext.GetType().GetProperty(propName); SettingsEntryAttribute settingsEntry = prop.GetCustomAttribute <SettingsEntryAttribute>(); element.Text = settingsEntry.DisplayName; element.ToolTip = settingsEntry.Tooltip; } }
static void OnTargetSet(DependencyObject depObj, DependencyPropertyChangedEventArgs e) { if (depObj is TextBlock element && e.NewValue is string propName && !String.IsNullOrEmpty(propName)) { if (element.DataContext != null) { PropertyInfo prop = element.DataContext.GetType().GetProperty(propName); SettingsEntryAttribute settingsEntry = prop.GetCustomAttribute <SettingsEntryAttribute>(); element.Text = settingsEntry.DisplayName; element.ToolTip = settingsEntry.Tooltip; } else { element.DataContextChanged += OnDataContextChanged; } } }
private static object GetDefaultValue(SettingsEntryAttribute attribute, Type settingsType) { var defaultValue = attribute.DefaultValue; if (defaultValue != null) { var defaultValueType = defaultValue.GetType(); if (settingsType == typeof(DateTime) && defaultValueType == typeof(string)) { defaultValue = DateTime.Parse((string)defaultValue); } if (settingsType == typeof(TimeSpan) && defaultValueType == typeof(string)) { defaultValue = TimeSpan.Parse((string)defaultValue); } } return(defaultValue); }