Is it possible to include an event handler reference in a data template that is not associated with the code-behind where the event handler is defined? I'm getting a PARSER-BAD-PROPERTY-VALUE when trying to pull this off.
For example, let's say I have this very simple XAML.
page.xaml
<DataTemplate x:Key="ItemsTemplate">
<HyperlinkButton Click="HyperlinkButton_Click" />
</DataTemplate>
<ItemsControl ItemTemplate="{StaticResource ItemsTemplate}" />
This works fine since the event handler HyperlinkButton_Click is in the code-behind for page.xaml.
BUT … when I move the data template to another file …
resources.xaml
<DataTemplate x:Key="ItemsTemplate">
<HyperlinkButton Click="HyperlinkButton_Click" />
</DataTemplate>
page.xaml
<ItemsControl ItemTemplate="{StaticResource ItemsTemplate}" />
… Silverlight seems to lose track of what I'm doing and I get the PARSER-BAD-PROPERTY-VALUE error.
Best Answer
I know you can not dynamically add controls that have event handlers, like in (XamlReader.Load(<"Button Click="handler".../>)). So maybe that's the reason why the Click in the external file does not work.