Reputation: 3
First let me say I've been working in WPF for a long time. I've defined and used many AttachedProperties but this one has me stumped. Maybe I'm just overlooking something simple and I need someone else's eyes to see it.
I have an AttachedProperty defined just like any other AttachedProperty:
public static readonly DependencyProperty NullAdornerStringProperty =
DependencyProperty.RegisterAttached(
"NullAdornerString",
typeof(string),
typeof(NullTextAdorner),
new FrameworkPropertyMetadata(null, OnNullAdornerStringChanged));
public static void SetNullAdornerStringProperty(DependencyObject obj, string nullAdornerString)
{
obj.SetValue(NullAdornerStringProperty, nullAdornerString);
}
public static string GetNullAdornerStringProperty(DependencyObject obj)
{
return (string)obj.GetValue(NullAdornerStringProperty);
}
NOTE: NullTextAdorner derives from DependencyOject and no errors from my property changed event handler.
BUT, when I go to use that AP in XAML, I can only reference it as "NullAdornerStringProperty" which is unusual because the actual property name is registered as "NullAdornerString". Any other AP I've made in the past is always referenced in XAML by the registered name, not the full name of the property.
I can actually assign a static string to the property in xaml like this and it works just fine at runtime:
<igWPF:XamTextEditor OSAdorners:NullTextAdorner.NullAdornerStringProperty="The Value is Null!!" />
BUT I can't set it with a binding expression
<igWPF:XamTextEditor OSAdorners:NullTextAdorner.NullAdornerStringProperty="{Binding SomeProperty}" />
This gives me a runtime error:
A 'Binding' cannot be set on the 'SetNullAdornerStringProperty' property of type 'XamTextEditor'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
If I force the XAML to be what it should be,
<igWPF:XamTextEditor OSAdorners:NullTextAdorner.NullAdornerString="{Binding SomeProperty}"
VS tells me it can't find the property:
The attachable property 'NullAdornerString' was not found in type 'NullTextAdorner'.
It doesn't matter what type of control I set the property on. I always get this behavior. This is in an assembly that has other attached properties that work correctly and can use bindings. I even moved this AP to a class with other APs that work and still got this behavior. I hope someone will see something that I'm blind to at the moment.
Thanks in advance.
Upvotes: 0
Views: 121
Reputation: 8791
Oh since when are attached properties defined that way my friend?
You are not following the naming conventions.
SetNullAdornerStringProperty - the property part shall be removed away.
Same goes for GetNullAdornerStringProperty - Remove the property part.
Upvotes: 3