Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

DataGrid: Missing template. Cannot initialize System.Wi... RSS

7 replies

Last post Sep 23, 2011 06:40 AM by khurram.ali

(0)
  • kobruleht

    kobruleht

    Member

    384 Points

    1083 Posts

    DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Feb 20, 2009 07:40 PM | LINK

    After adding button column to subclassed DataGrid I got strange exception

    TypeInitializationException: The type initializer for 'Missing template.  Cannot initialize System.Windows.Controls.DataGridTemplateColumn.' threw an exception.

    several times. How to fix ?

    Andrus. 

    Sublcassed DataGrid contains method to create grid column:

    namespace AWSilverlightLOB.Controls {

    public class DataGridBase : DataGrid {

    internal void Create() {

    ItemsSource = ...

    DataGridTemplateColumn xtemplateColumn = new DataGridTemplateColumn();xtemplateColumn.CellTemplate = (DataTemplate)Resources["ColButton"];

    Columns.Add(xtemplateColumn);

    }}}

    namespace AWSilverlightLOB.Content {

    public abstract class UserControlBase : UserControl { }

    }

     

    XAML:

     

    <userControlBase:UserControlBase x:Class="Cart.ProductSearch"

    xmlns="http://schemas.microsoft.com/client/2007"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:userControlBase="clr-namespace:AWSilverlightLOB.Content"

    >

    <UserControl.Resources>

    <DataTemplate x:Key="ColButton" >

    <Button Click="AddToCart_Click" Content="Add to cart" Margin="3,0" />

    </DataTemplate>

     

    Exception occurs in browser if DataGrid is being rendered:

    ---------------------------
    Windows Internet Explorer
    ---------------------------
    Problem
    Code: 4004   
    Category: ManagedRuntimeError      
    Message: System.TypeInitializationException: The type initializer for 'Missing template.  Cannot initialize System.Windows.Controls.DataGridTemplateColumn.' threw an exception.

       at System.Windows.Controls.DataGrid.PopulateCellContent(Boolean forceTemplating, Boolean isCellEdited, DataGridColumn dataGridColumn, DataGridRow dataGridRow, DataGridCell dataGridCell)

       at System.Windows.Controls.DataGrid.AddNewCellPrivate(DataGridRow row, DataGridColumn column)

       at System.Windows.Controls.DataGrid.CompleteCellsCollection(DataGridRow dataGridRow)

       at System.Windows.Controls.DataGrid.GenerateRow(Int32 rowIndex)

       at System.Windows.Controls.DataGrid.GetEdgedExactRowHeight(Int32 rowIndex)

       at System.Windows.Controls.DataGrid.UpdateDisplayedRows(Int32 newFirstDisplayedRowIndex, Double displayHeight)

       at System.Windows.Controls.DataGrid.ComputeScrollBarsLayout()

       at System.Windows.Controls.DataGrid.MeasureOverride(Size availableSize)

       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)    

     

    Microsoft Community Contributor
  • Allen Chen – MSFT

    Allen Chen –...

    Star

    14215 Points

    1854 Posts

    Microsoft

    Re: DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Feb 23, 2009 09:21 AM | LINK

    Hi,

    Could you share a demo project for me to test?

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • kobruleht

    kobruleht

    Member

    384 Points

    1083 Posts

    Re: DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Feb 23, 2009 10:31 AM | LINK

    The issue was causes by not calling InitializeComponent() in subclassed DataGrid constructor.

    Resources[] returns null and this causes this strange error message. After adding DataGridBase constructor with call to InitializeComponent()

    problem has solved.

    I you still needs demo project, please let me know.

    Andrus-

    Microsoft Community Contributor
  • amyo

    amyo

    Star

    8312 Points

    1083 Posts

    Re: DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Feb 23, 2009 11:55 AM | LINK

    kobruleht

    xtemplateColumn.CellTemplate = (DataTemplate)Resources["ColButton"];

    Is ColButton DataTemplete inside DataGridBase or inside ProductSearch?

    In your case you are accessing ColButton DataTemplete of DataGridBase.

     

    Hope this will help you.

    Please Mark as Answer if this helps you.

  • kobruleht

    kobruleht

    Member

    384 Points

    1083 Posts

    Re: DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Feb 23, 2009 12:06 PM | LINK

    amyo,

    DataTemplate is inside DataGridBase .

    Adding InitializeComponent() call to DataGridBase constructor solves issue.

    Andrus.

    Microsoft Community Contributor
  • SASilver

    SASilver

    Member

    46 Points

    29 Posts

    Re: DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Aug 18, 2009 04:28 PM | LINK

    Hello,

    I am using an AutoCompleteBox in a DataGrid. Im setting the values as a property of the object that the grid is bound to.

    I have an implementation of this in another page that works fine. But since shifting to SIlverlight 3 i've been getting this error. The data is perfect and is bound to ItemsSource but does not display the data instead shows this javascript error.

     "The type initializer for 'Missing template.  Cannot initialize System.Windows.Controls.DataGridTemplateColumn.' threw an exception. "

    Any suggestions?

    Thanks

     

     

  • khurram.ali

    khurram.ali

    Member

    20 Points

    15 Posts

    Re: DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Sep 23, 2011 06:40 AM | LINK

    can you please elaborate your point or share the some code? :S
  • khurram.ali

    khurram.ali

    Member

    20 Points

    15 Posts

    Re: DataGrid: Missing template. Cannot initialize System.Windows.Controls.DataGridTemplateColumn

    Sep 23, 2011 06:40 AM | LINK

    kobruleht

    The issue was causes by not calling InitializeComponent() in subclassed DataGrid constructor.

    Resources[] returns null and this causes this strange error message. After adding DataGridBase constructor with call to InitializeComponent()

    problem has solved.

    I you still needs demo project, please let me know.

    Andrus-

    can uou please elaborate or share some code? :S