DataGrid DataTemplate

Sep 1, 2008 at 8:52 AM
Edited Sep 1, 2008 at 8:53 AM
I need to create DataTemplate through code.

                    DataGridTemplateColumn column = new DataGridTemplateColumn();
                    column.Header = property.Alias;

                    FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Button));

                    factory.Name = "PopupButton";
                    factory.SetValue(Button.ContentProperty, "+");
                    factory.SetBinding(Button.DataContextProperty, new Binding(property.LinkedBO.Name) { Mode = BindingMode.OneWay });
                    factory.AddHandler(Button.ClickEvent, new RoutedEventHandler(ManyToOneButton_Click));

                    DataTemplate cellTemplate = new DataTemplate();
                    cellTemplate.VisualTree = factory;

                    column.CellTemplate = cellTemplate;


 But unfortunately WPF DataGrid doesn't work with such a DataTemplate. It throughs an exception "FrameworkElementFactory must be in a sealed template for this operation".
Any ideas?

Sep 3, 2008 at 12:34 AM
Try sealing the template after you set it, column.CellTemplate.Seal().
Sep 4, 2008 at 3:15 AM
Vincent Sibal, I have no exception now, but my event handler is not raised :(I think it's because of sealing the template (but I'm not sure).
Sep 5, 2008 at 6:11 AM
Does anybody has this problem? Or may be I make something wrong...
Sep 8, 2008 at 3:02 PM
The issue has been logged and we are taking a look at it.  Sorry for the inconvenience.