I've run into an odd problem with attached properties where when I assign the property name in the call to RegisterAttached and name properly for the name of the attached property (say TranslateProperty and "Translate") the code for the attached property implementation doesn't fire. Just doesn't get called. If I change the string name to anything other than Translate (say "Translate_") the code gets called just fine.
Here's the implementation:
public class TranslateExtension : DependencyObject {
public static readonly DependencyProperty TranslateProperty =
DependencyProperty.RegisterAttached("Translate_",
typeof(bool),
typeof(TranslateExtension),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public static void SetTranslate(UIElement element, bool value)
{
AssignKeys(element);
element.SetValue(TranslateProperty, value);
}
public static bool GetTranslate(UIElement element)
{
return (bool)element.GetValue(TranslateProperty);
}
public bool Translate
{
set { base.SetValue( TranslateProperty, value); }
}
...
}
The above actually works because the property in the string is Translate_. If I change the string value to "Translate" it failed.
I have 2 other attached properties in the same class and they exhibit exactly the same behavior - same name as the AttachedProperty and they don't get called. Name it something else and it works.
I'm not sure what's going on here. My code is actually working with the invalid names, but I don't understand why, and more importantly I'm not sure if this wrong naming causes any side effects.
Can anybody see whether I'm overlooking something painfully obvious? I've revisited a few examples in articles of AttachedProperties and I don't see those implementations using special names - they always name the string property the same as the attached properties.