locked
How to applay template to DataGridTemplateColumn.CellTemplate for DataGrid that its columns detrmine on run time by binding RRS feed

  • Question

  • How to apply template to DataGridTemplateColumn.CellTemplate for DataGrid that its columns determine on run time by binding of

    ItemSource.

    I mean that I cannot define in XAML colums.

     <DataGrid ItemsSource="{Binding MeasurementsTbl}" HeadersVisibility="None" Grid.Column="3" Margin="18 0 0 0" VerticalAlignment="Center" BorderThickness="0" Background="Transparent" >                                           

     </DataGrid>

    Tomer

    Thanks,


    tomer trojman

    Wednesday, May 28, 2014 10:50 AM

Answers

  • A DataGridTemplateColumn is never generated for an automatically generated column. Any System.String (string) property of the class that represents the objects in the ItemsSource collection will generate a DataGridTextColumn, a System.Boolean (bool) property will generate a DataGridCheckBoxColumn, a System.Uri property will generate a DataGridHyperlinkColumn and an enumeration type (System.Enum) property will generate a DataGridComboBoxColumn.

    If you don't define the columns explicitly in XAML nor write any custom code, you will never get any DataGridTemplateColumn. You could define your CellTemplates in XAML and then handle the AutoGeneratingColumn event of the DataGrid and create the columns in this event handler and assing them the CellTemplates from the XAML but you still need to create the columns, either in XAML or in code. Please refer to the following page for more information: http://msdn.microsoft.com/en-us/library/cc903950(v=vs.95).aspx

    • Marked as answer by tomert77 Wednesday, May 28, 2014 2:48 PM
    Wednesday, May 28, 2014 11:04 AM

All replies

  • A DataGridTemplateColumn is never generated for an automatically generated column. Any System.String (string) property of the class that represents the objects in the ItemsSource collection will generate a DataGridTextColumn, a System.Boolean (bool) property will generate a DataGridCheckBoxColumn, a System.Uri property will generate a DataGridHyperlinkColumn and an enumeration type (System.Enum) property will generate a DataGridComboBoxColumn.

    If you don't define the columns explicitly in XAML nor write any custom code, you will never get any DataGridTemplateColumn. You could define your CellTemplates in XAML and then handle the AutoGeneratingColumn event of the DataGrid and create the columns in this event handler and assing them the CellTemplates from the XAML but you still need to create the columns, either in XAML or in code. Please refer to the following page for more information: http://msdn.microsoft.com/en-us/library/cc903950(v=vs.95).aspx

    • Marked as answer by tomert77 Wednesday, May 28, 2014 2:48 PM
    Wednesday, May 28, 2014 11:04 AM
  • Thanks very much.

    tomer trojman

    Wednesday, May 28, 2014 2:48 PM