views:

165

answers:

1

I've created a custom WPF control that performs some animations on dynamically created objects. I use SetTargetName() to set animation target. (I know about SetTarget() but I need to support .NET 3.5 without SP1.) Obviously before that I need to register object names with RegisterName(). Everything works fine when my control is placed normally as an object in a window or page.

But when I add my control to a DataTemplate I get a NullReferenceException on a call to RegisterName().

The top of stack trace looks like this

at System.Windows.TemplateNameScope.System.Windows.Markup.INameScope.RegisterName(String name, Object scopedElement)
at System.Windows.FrameworkElement.RegisterName(String name, Object scopedElement)

Both of the parameters I pass to RegisterName are not null and the object on which I call it isn't null. So something else must be null internally and I have no idea what to do about it. There's no information on TemplateNameScope anywhere either.

Any pointers are highly appreciated.

+1  A: 

i experienced this today. my control does not directly reference the binding of the datatemplate, so i get this exception. adding a token reference to the binding fixes the issue. For Example in the DataTemplate xaml for my custom control:

DataContext="{Binding}"