NullReferenceException calls RegisterName () from control in DataTemplate

I created my own WPF control that does some animations on dynamically created objects. I am using SetTargetName () to set the target of the animation. (I know about SetTarget (), but I need to support .NET 3.5 without SP1.) Obviously, before that I need to register the object names with RegisterName (). Everything works fine when my control is usually placed as an object in a window or on a page.

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

The top of the stack trace looks like this:

at System.Windows.TemplateNameScope.System.Windows.Markup.INameScope.RegisterName (row name, scopedElement object)
at System.Windows.FrameworkElement.RegisterName (row name, scopedElement object)

Both parameters I pass to RegisterName are not null and the object I am calling it on is not null. So something else must be null internally and I don't know what to do about it. There is also no information about TemplateNameScope.

Any pointers are much appreciated.

0


a source to share


1 answer


I experienced this today. my control does not directly reference the datatemplate data binding, so I am getting this exception. adding a token reference to the binding fixed the issue. For example, in the DataTemplate xaml for my custom control:



DataContext = "{Binding}"

+1


a source







All Articles