This project has moved and is read-only. For the latest updates, please go here.

View a datagrid column's data type

Dec 11, 2014 at 11:47 AM
Edited Dec 11, 2014 at 11:48 AM
As the title says, does anyone know of a way of accessing what data type a column contains? I would like to set custom cell templates and column headers depending on the column data type, but can't see a way of finding out what they are.
My columns are created dynamically depending on itemproperties - they cannot be hard coded since there is the possibility of them being changed.
Developer
Dec 11, 2014 at 4:24 PM
Hi,

Maybe you could use "CellContentTemplateSelector" from Column :
 <StackPanel>
      <Button Content="Change ItemsSource"
              Click="Button_Click" />
      <xcdg:DataGridControl x:Name="_datagrid">
         <xcdg:DataGridControl.Resources>
            <DataTemplate x:Key="DateTimeTemplate">
               <TextBox Text="{Binding .}"
                        Background="Green" />
            </DataTemplate>

            <DataTemplate x:Key="DoubleTemplate">
               <TextBox Text="{Binding .}"
                        Background="Red" />
            </DataTemplate>
         </xcdg:DataGridControl.Resources>
         <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="StartWorkAt"
                         CellContentTemplateSelector="{x:Static local:CellDataTemplateSelector.Singleton}">
            </xcdg:Column>
         </xcdg:DataGridControl.Columns>
      </xcdg:DataGridControl>
</StackPanel>

 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      _datagrid.ItemsSource = new List<MyData>()
      {
        new MyData() { LastName = "TEST", StartWorkAt = new DateTime( 1987, 12, 8, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0) },
        new MyData() { LastName = "TEST2", StartWorkAt = new DateTime( 1987, 12, 9, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
        new MyData() { LastName = "TEST3", StartWorkAt = new DateTime( 1987, 12, 10, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
        new MyData() { LastName = "TEST4", StartWorkAt = new DateTime( 1987, 12, 11, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
        new MyData() { LastName = "TEST5", StartWorkAt = new DateTime( 1987, 12, 12, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
      };
    }

    private void Button_Click( object sender, RoutedEventArgs e )
    {
      _datagrid.ItemsSource = new List<MyData2>()
      {
        new MyData2() { FirstName = "Tom", StartWorkAt = 10d},
        new MyData2() { FirstName = "Kathy", StartWorkAt = 8d  },
        new MyData2() { FirstName = "Jenny", StartWorkAt = 7d  },
        new MyData2() { FirstName = "Brian", StartWorkAt = 8d },
      };
    }
  }

  public class MyData
  {
    public string LastName
    {
      get;
      set;
    }

    public DateTime StartWorkAt
    {
      get;
      set;
    }

    public DateTime TheSecondOne
    {
      get;
      set;
    }
  }

  public class MyData2
  {
    public string FirstName
    {
      get;
      set;
    }

    public double StartWorkAt
    {
      get;
      set;
    }
  }

  public class CellDataTemplateSelector : DataTemplateSelector
  {
    #region Singleton Property

    public static CellDataTemplateSelector Singleton
    {
      get
      {
        if( _singleton == null )
          _singleton = new CellDataTemplateSelector();

        return _singleton;
      }
    }

    private static CellDataTemplateSelector _singleton;

    #endregion Singleton Property

    #region PUBLIC METHODS

    public override DataTemplate SelectTemplate( object item, DependencyObject container )
    {
      DataGridContext dataGridContext = DataGridControl.GetDataGridContext( container );

      DataGridControl parentDataGrid = ( dataGridContext != null )
        ? dataGridContext.DataGridControl
        : null;

      if( parentDataGrid == null || item == null)
        return base.SelectTemplate( item, container );

      if( item is DateTime )
      {
        // When the grid's view is TableView, we want to right-align the value.
        return parentDataGrid.FindResource( "DateTimeTemplate" ) as DataTemplate;
      }

      return parentDataGrid.FindResource( "DoubleTemplate" ) as DataTemplate;
    }

    #endregion PUBLIC METHODS
  }
Dec 12, 2014 at 10:24 AM
The problem with this solution is in my program the columns are being created dynamically. I have no idea what field names there will be until runtime so I cannot code them in the xaml as you have shown.
Developer
Dec 12, 2014 at 1:33 PM
Hi,

I see.
But when the column is created dynamically, can you add the property CellContentTemplateSelector = new CellDataTemplateSelector to it ? CellDataTemplateSelector would contain all possible types an point to all DataTemplates defined in XAML.