This is a migrated thread and some comments may be shown as answers.

Problem with DataTemplate and handler

1 Answer 126 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Michael asked on 14 Jan 2019, 08:15 AM




In our project we must create a DataTemplate for a grid cell programmatically. Everything works perfect, but a click to a button within the template does not work.

Here is a snippet of the code:

var cellEditTemplate = new DataTemplate();

var dockPanel = new FrameworkElementFactory( typeof( DockPanel ) );
dockPanel.SetValue( DockPanel.LastChildFillProperty, true );

var textBlock = new FrameworkElementFactory( typeof( TextBlock ) );
textBlock.SetValue( FrameworkElement.StyleProperty, textBlockStyle );
textBlock.SetBinding( TextBlock.TextProperty, binding );
textBlock.SetValue( DockPanel.DockProperty, Dock.Left );

var button = new FrameworkElementFactory( typeof( RadButton ) );
button.SetValue( FrameworkElement.StyleProperty, buttonStyle );
button.SetValue( ContentControl.ContentProperty, "Press me" );
button.SetValue( FrameworkElement.MinWidthProperty, 0.0 );
button.SetValue( DockPanel.DockProperty, Dock.Right );
button.AddHandler( Button.ClickEvent,
                   new RoutedEventHandler( ( sender, args ) =>
                                               int i = 0;
                                           } ) );

button.SetValue( Button.CommandProperty );

dockPanel.AppendChild( button );
dockPanel.AppendChild( textBlock );

cellEditTemplate.VisualTree = dockPanel;


The event handler is not called.


Best regards,




1 Answer, 1 is accepted

Sort by
Vladimir Stoyanov
Telerik team
answered on 15 Jan 2019, 01:24 PM
Hello Michael,

Thank you for the provided code snippet. 

What you have observed seems to be a WPF framework limitation. You can check out the following forum thread: FrameworkElementFactory.AddHandler not working, where this scenario has been discussed. In order for the event handler to be called as intended, you can define the DataTemplate as a resource in xaml.

Hope you find this helpful.

Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Asked by
Top achievements
Rank 1
Answers by
Vladimir Stoyanov
Telerik team
Share this question