I have a DependencyObject (an Interactivity Behavior), and I'd like to get its x:Name (just get, not set) from code. Is it possible?
EDIT: Following AnthonyWJones's answer:
I've inserted the following code into my base behavior:
[EditorBrowsable(EditorBrowsableState.Never)]
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(BaseBehavior<T>), new PropertyMetadata(null));
I've given my behaviors x:Name, yet the Name property doesn't get filled.
If the class deriving from
DependencyObjectdoes not expose aNameproperty then you cannot determine the assignedx:Name. Thex:Namevalue is store only in an internal object tree and there is no API to resolve value (the object) back to a key value (the name).However if this is your own behaviour then simply add a
Namedependency property to your behaviour. x:Name will assign is value to aNameproperty if present.If this an existing behaviour you may be able to inherit from it to create a new class that has a
Nameproperty. Unfortunately some behaviours are sealed so you can't always do this.