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
DependencyObject
does not expose aName
property then you cannot determine the assignedx:Name
. Thex:Name
value 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
Name
dependency property to your behaviour. x:Name will assign is value to aName
property if present.If this an existing behaviour you may be able to inherit from it to create a new class that has a
Name
property. Unfortunately some behaviours are sealed so you can't always do this.