-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/auto watermark #2722
Feature/auto watermark #2722
Conversation
This commit introduces the possibility to retrieve the automark text directly from the property that is used for data binding
@xxMUROxx We can do this for .Net 4 too, look at this here which works... private static void OnControlWithAutoWatermarkSupportLoaded(object o, RoutedEventArgs routedEventArgs)
{
FrameworkElement obj = (FrameworkElement)o;
obj.Loaded -= OnControlWithAutoWatermarkSupportLoaded;
DependencyProperty dependencyProperty;
if (!AutoWatermarkPropertyMapping.TryGetValue(obj.GetType(), out dependencyProperty))
{
throw new NotSupportedException($"{nameof(AutoWatermarkProperty)} is not supported for {obj.GetType()}");
}
var binding = obj.GetBindingExpression(dependencyProperty);
if (binding != null)
{
var dataItem = binding.DataItem.GetType();
#if NET4_5
var propertyName = binding.ResolvedSourcePropertyName;
#else
var propertyName = binding.ParentBinding?.Path?.Path;
#endif
if (propertyName != null)
{
var property = dataItem.GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
if (property != null)
{
#if NET4_5
var attribute = property.GetCustomAttribute<WatermarkAttribute>();
#else
var attribute = property.GetCustomAttributes(typeof(WatermarkAttribute), false).FirstOrDefault() as WatermarkAttribute;
#endif
if (attribute != null)
{
obj.SetValue(WatermarkProperty, attribute.Caption);
}
}
}
}
}
#endif But we must make a note to the users, that this feature uses reflection, which can have performance issues if you use this on many controls with watermark in a UI. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, can you revert the Xaml style changes? Thx
FrameworkElement element = d as FrameworkElement; | ||
if (element != null) | ||
{ | ||
element.Loaded += OnControlWithAutoWatermarkSupportLoaded; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible memory leak...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the event subscription? Shouldn't it be enough to unsubscribe directly after the event was raised the first time?
public string Caption { get; set; } | ||
} | ||
#endif | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it necessary to introduce a new attribute? Is the Display attribute too bad?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, of course, it is not.
93b391d
to
df4eb09
Compare
@punker76 XAML-Style change was introduces unintentionally. I will revert them |
If control is already loaded, directly call the method for retrieving the information. If deactivated unsubscribe from event.
e.g. "{Binding Property.OtherProperty}"
e.g. PropertyWithCollection[0]. This is not supported
@punker76 reverted xaml, added .net4 support and hopefully removed memory leak |
@xxMUROxx It seems this feature is done. So can I merge it? |
@punker76 yes |
Thanks guys |
AutoWatermark
is able to get theDisplayAttribute
from the bound property in following cases:"{Binding Path=Property}"
"{Binding Path=Property.SubProperty}"
"{Binding Path=CollectionProperty}"
"{Binding Path=CollectionProperty[0].SubProperty}"
"{Binding Path=CollectionProperty[0]}"
Tasks
closes #2712