<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Scott Morrison : Value Converters</title><link>http://blogs.msdn.com/scmorris/archive/tags/Value+Converters/default.aspx</link><description>Tags: Value Converters</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Defining Silverlight DataGrid Columns at Runtime</title><link>http://blogs.msdn.com/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns-at-runtime.aspx</link><pubDate>Tue, 15 Apr 2008 01:25:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8395118</guid><dc:creator>scmorris</dc:creator><slash:comments>34</slash:comments><comments>http://blogs.msdn.com/scmorris/comments/8395118.aspx</comments><wfw:commentRss>http://blogs.msdn.com/scmorris/commentrss.aspx?PostID=8395118</wfw:commentRss><description>&lt;table cellspacing="0" cellpadding="5" width="100%" bgcolor="#fff8c9" border="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top"&gt;This post has been updated to work with the &lt;a href="http://silverlight.net/GetStarted/" target="_blank"&gt;RTW version of the Silverlight 2 DataGrid&lt;/a&gt;.&amp;#160; The code examples are not guaranteed to work with previous Beta versions of the DataGrid. &lt;a href="http://blogs.msdn.com/scmorris/archive/2008/10/14/silverlight-2-datagrid-is-released.aspx" target="_blank"&gt;Read more about the features that the Silverlight 2 DataGrid has to offer...&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Now that you know the &lt;a href="http://blogs.msdn.com/scmorris/archive/2008/03/21/using-the-silverlight-datagrid.aspx" target="_blank"&gt;basics of the Silverlight DataGrid&lt;/a&gt; and how to &lt;a href="http://blogs.msdn.com/scmorris/archive/2008/03/27/defining-columns-for-a-silverlight-datagrid.aspx" target="_blank"&gt;specify the Columns in XAML&lt;/a&gt;, you might want to customize your DataGrid's columns at runtime.&lt;/p&gt;  &lt;p&gt;This process is pretty straight forward, so instead of doing the usual end-to-end walk through, I'm going to provide you with a Rosetta Stone between the static XAML form and the dynamic C#/VB form for &lt;a href="http://blogs.msdn.com/scmorris/archive/2008/03/27/defining-columns-for-a-silverlight-datagrid.aspx" target="_blank"&gt;the scenario in the last post&lt;/a&gt;.&lt;/p&gt;  &lt;h4&gt;Defining a DataGrid&lt;/h4&gt;  &lt;p&gt;For any of these columns to be useful you are going to first need a DataGrid to add them to.&amp;#160; The following creates a DataGrid, adds it as a child of the root layout Grid, and sets its ItemsSource to a collection called &amp;quot;source&amp;quot;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DataGrid &lt;/span&gt;targetDataGrid = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataGrid&lt;/span&gt;();
targetDataGrid.ItemsSource = source;
targetDataGrid.AutoGenerateColumns = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
LayoutRoot.Children.Add(targetDataGrid);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;TargetDataGrid &lt;span style="color: blue"&gt;As New &lt;/span&gt;DataGrid
TargetDataGrid.ItemsSource = Source
TargetDataGrid.AutoGenerateColumns = &lt;span style="color: blue"&gt;False&lt;/span&gt;
LayoutRoot.Children.Add(TargetDataGrid)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h4&gt;&amp;#160;&lt;/h4&gt;

&lt;h4&gt;Defining a DataGrid Text Column&lt;/h4&gt;

&lt;p&gt;The following creates a DataGridTextColumn bound to the FirstName property with a header of &amp;quot;First Name&amp;quot;, and adds it to the columns collection of the DataGrid named &amp;quot;targetDataGrid&amp;quot;.&lt;/p&gt;

&lt;h5&gt;Static&lt;/h5&gt;

&lt;pre class="code"&gt; &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;targetDataGrid&amp;quot;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
         &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;First Name&amp;quot; 
             &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;FirstName&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
     &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
 &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h5&gt;Dynamic&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Windows.Data;&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;span style="color: #2b91af"&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DataGridTextColumn &lt;/span&gt;textColumn = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataGridTextColumn&lt;/span&gt;();
textColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;First Name&amp;quot;&lt;/span&gt;;
textColumn.Binding = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Binding&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;);
targetDataGrid.Columns.Add(textColumn);&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Imports &lt;/span&gt;System.Windows.Data&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;TextColumn &lt;span style="color: blue"&gt;As New &lt;/span&gt;DataGridTextColumn
TextColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;First Name&amp;quot;
&lt;/span&gt;TextColumn.Binding = &lt;span style="color: blue"&gt;New &lt;/span&gt;Binding(&lt;span style="color: #a31515"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;)
TargetDataGrid.Columns.Add(TextColumn)&lt;/pre&gt;

&lt;h4&gt;&amp;#160;&lt;/h4&gt;

&lt;h4&gt;Defining a DataGrid CheckBox Column&lt;/h4&gt;

&lt;p&gt;The following creates a DataGridCheckColumn bound to the Available property with a header of &amp;quot;Available&amp;quot;, and adds it to the columns collection of the DataGrid named &amp;quot;targetDataGrid&amp;quot;.&lt;/p&gt;

&lt;h5&gt;Static&lt;/h5&gt;

&lt;pre class="code"&gt; &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;targetDataGrid&amp;quot;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
         &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridCheckBoxColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Available &amp;quot; 
             &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Available&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
     &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
 &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h5&gt;Dynamic&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Windows.Data;&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DataGridCheckBoxColumn &lt;/span&gt;checkBoxColumn = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataGridCheckBoxColumn&lt;/span&gt;();
checkBoxColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;Available&amp;quot;&lt;/span&gt;;
checkBoxColumn.Binding = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Binding&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Available&amp;quot;&lt;/span&gt;);
targetDataGrid.Columns.Add(checkBoxColumn);&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Imports &lt;/span&gt;System.Windows.Data&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;CheckBoxColumn &lt;span style="color: blue"&gt;As New &lt;/span&gt;DataGridCheckBoxColumn
CheckBoxColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;Available&amp;quot;
&lt;/span&gt;CheckBoxColumn.Binding = &lt;span style="color: blue"&gt;New &lt;/span&gt;Binding(&lt;span style="color: #a31515"&gt;&amp;quot;Available&amp;quot;&lt;/span&gt;)
TargetDataGrid.Columns.Add(CheckBoxColumn)&lt;/pre&gt;

&lt;h4&gt;&amp;#160;&lt;/h4&gt;
&lt;a name="template"&gt;
  &lt;h4&gt;Defining a DataGrid Template Column&lt;/h4&gt;
&lt;/a&gt;

&lt;p&gt;The following creates a DataGridTemplateColumn bound to the Birthday property with a header of &amp;quot;Birthday&amp;quot;, and adds it to the columns collection of the DataGrid named &amp;quot;targetDataGrid&amp;quot;.&lt;/p&gt;

&lt;h5&gt;Static&lt;/h5&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DateTimeConverter &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Key&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;DateConverter&amp;quot; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;...&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;targetDataGrid&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;AutoGenerateColumns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;False&amp;quot; &amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Birthday&amp;quot;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock 
                        &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Birthday&lt;/span&gt;&lt;span style="color: blue"&gt;, 
                        &lt;/span&gt;&lt;span style="color: red"&gt;Converter&lt;/span&gt;&lt;span style="color: blue"&gt;={&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;DateConverter&lt;/span&gt;&lt;span style="color: blue"&gt;}}&amp;quot; 
&lt;/span&gt;&lt;span style="color: blue"&gt;                        &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;4&amp;quot;/&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellEditingTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;basics&lt;span style="color: blue"&gt;:&lt;/span&gt;DatePicker 
                        &lt;/span&gt;&lt;span style="color: red"&gt;SelectedDate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Birthday&lt;/span&gt;&lt;span style="color: blue"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;Mode&lt;/span&gt;&lt;span style="color: blue"&gt;=TwoWay}&amp;quot; /&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellEditingTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h5&gt;Dynamic&lt;/h5&gt;

&lt;p&gt;There are two ways to dynamically create a template column for a DataGrid.&amp;#160; One is to load in the CellTemplate and CellEditingTemplates as DataTemplates from resources, and the other is to construct the DataTemplates on the fly using XamlReader.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Resources Method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method creates the CellTemplate DataTemplate and the CellEditingTemplate DataTemplate in XAML and stores them as named resources.&amp;#160; Then when the column is created the DataTemplates are used.&lt;/p&gt;

&lt;p&gt;Use the below XAML to create the DataTemplates as resources to support the code for this method.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DateTimeConverter &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Key&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;DateConverter&amp;quot; /&amp;gt;

    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Key&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myCellTemplate&amp;quot;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock 
            &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Birthday&lt;/span&gt;&lt;span style="color: blue"&gt;, 
            &lt;/span&gt;&lt;span style="color: red"&gt;Converter&lt;/span&gt;&lt;span style="color: blue"&gt;={&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;DateConverter&lt;/span&gt;&lt;span style="color: blue"&gt;}}&amp;quot; 
&lt;/span&gt;&lt;span style="color: blue"&gt;            &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;4&amp;quot;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Key&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myCellEditingTemplate&amp;quot;&amp;gt;
        &amp;lt;&lt;span style="color: #a31515"&gt;basics&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DatePicker 
            &lt;/span&gt;&lt;span style="color: red"&gt;SelectedDate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Birthday&lt;/span&gt;&lt;span style="color: blue"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;Mode&lt;/span&gt;&lt;span style="color: blue"&gt;=TwoWay}&amp;quot; /&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Windows.Data;&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DataGridTemplateColumn &lt;/span&gt;templateColumn = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataGridTemplateColumn&lt;/span&gt;();
templateColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;Birthday&amp;quot;&lt;/span&gt;;
templateColumn.CellTemplate = (&lt;span style="color: #2b91af"&gt;DataTemplate&lt;/span&gt;)Resources[&lt;span style="color: #a31515"&gt;&amp;quot;myCellTemplate&amp;quot;&lt;/span&gt;];
templateColumn.CellEditingTemplate = 
                       (&lt;span style="color: #2b91af"&gt;DataTemplate&lt;/span&gt;)Resources[&lt;span style="color: #a31515"&gt;&amp;quot;myCellEditingTemplate&amp;quot;&lt;/span&gt;];
targetDataGrid.Columns.Add(templateColumn);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Imports &lt;/span&gt;System.Windows.Data&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;TemplateColumn &lt;span style="color: blue"&gt;As New &lt;/span&gt;DataGridTemplateColumn
TemplateColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;Birthday&amp;quot;
&lt;/span&gt;TemplateColumn.CellTemplate = Resources(&lt;span style="color: #a31515"&gt;&amp;quot;myCellTemplate&amp;quot;&lt;/span&gt;)
TemplateColumn.CellEditingTemplate = Resources(&lt;span style="color: #a31515"&gt;&amp;quot;myCellEditingTemplate&amp;quot;&lt;/span&gt;)
TargetDataGrid.Columns.Add(TemplateColumn)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;2. XamlReader Method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method creates the DataTemplate inline using the XamlReader class.&amp;#160; This class takes a string and parses it to try to build a visual tree.&amp;#160; In this case we are creating DataTemplates.&amp;#160; This method is especially useful if the DataTemplate itself has to be dynamic.&amp;#160; One example being if you wanted to modify what the element in the template was bound to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Warning:&lt;/em&gt; &lt;/strong&gt;This method is considerably more difficult than the resources method.&amp;#160; I recommend only using this if you need to dynamically create the DataTemplate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some things to watch out for:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;You will need to declare any XAML namespace that is used in the data template &lt;/li&gt;

  &lt;li&gt;Any custom XAML namespace needs to specify both the clr-namespace and the assembly &lt;/li&gt;

  &lt;li&gt;You cannot have white space between the xmlns: and the name of your namespace &lt;/li&gt;

  &lt;li&gt;External resources cannot be referenced, they need to be declared inline &lt;/li&gt;

  &lt;li&gt;The entire template is a single line, so if you get a XAML Parse exception, it will always say line 1, however the character is fairly accurate if you were to concatenate all of your lines. &lt;/li&gt;

  &lt;li&gt;When using the StringBuilder approach shown below, make sure that you have the correct white space at the end of a line so that it is correctly separated when concatenated with the next line. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that the warnings are out of the way, here is how you do the equivalent of the code above:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Windows.Data;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Windows.Markup;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Text;&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DataGridTemplateColumn &lt;/span&gt;templateColumn = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataGridTemplateColumn&lt;/span&gt;();
templateColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;Birthday&amp;quot;&lt;/span&gt;;

&lt;span style="color: #2b91af"&gt;StringBuilder &lt;/span&gt;CellTemp = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringBuilder&lt;/span&gt;();
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;DataTemplate &amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns='http://schemas.microsoft.com/winfx/&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;2006/xaml/presentation' &amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' &amp;quot;&lt;/span&gt;);

&lt;span style="color: green"&gt;//Be sure to replace &amp;quot;YourNamespace&amp;quot; and &amp;quot;YourAssembly&amp;quot; with your app's 
//actual namespace and assembly here
&lt;/span&gt;CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:local = 'clr-namespace:YourNamespace&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;;assembly=YourAssembly'&amp;gt;&amp;quot;&lt;/span&gt;);

CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;Grid&amp;gt;&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;Grid.Resources&amp;gt;&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;local:DateTimeConverter x:Key='DateConverter' /&amp;gt;&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/Grid.Resources&amp;gt;&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;TextBlock &amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;Text = '{Binding Birthday, &amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;Converter={StaticResource DateConverter}}' &amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;Margin='4'/&amp;gt;&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/Grid&amp;gt;&amp;quot;&lt;/span&gt;);
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/DataTemplate&amp;gt;&amp;quot;&lt;/span&gt;);

&lt;span style="color: #2b91af"&gt;StringBuilder &lt;/span&gt;CellETemp = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringBuilder&lt;/span&gt;();
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;DataTemplate &amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns='http://schemas.microsoft.com/winfx/&amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;2006/xaml/presentation' &amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' &amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:basics='clr-namespace:System.Windows.Controls;&amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;assembly=System.Windows.Controls' &amp;gt;&amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;basics:DatePicker &amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;SelectedDate='{Binding Birthday, Mode=TwoWay}' /&amp;gt;&amp;quot;&lt;/span&gt;);
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/DataTemplate&amp;gt;&amp;quot;&lt;/span&gt;);

templateColumn.CellTemplate =
    (&lt;span style="color: #2b91af"&gt;DataTemplate&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;XamlReader&lt;/span&gt;.Load(CellTemp.ToString());
templateColumn.CellEditingTemplate =
    (&lt;span style="color: #2b91af"&gt;DataTemplate&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;XamlReader&lt;/span&gt;.Load(CellETemp.ToString());
targetDataGrid.Columns.Add(templateColumn);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Imports &lt;/span&gt;System.Windows.Data
&lt;span style="color: blue"&gt;Imports &lt;/span&gt;System.Text
&lt;span style="color: blue"&gt;Imports &lt;/span&gt;System.Windows.Markup&lt;/pre&gt;

&lt;pre class="code"&gt;...&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;TemplateColumn &lt;span style="color: blue"&gt;As New &lt;/span&gt;DataGridTemplateColumn
TemplateColumn.Header = &lt;span style="color: #a31515"&gt;&amp;quot;Birthday&amp;quot;

&lt;/span&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;CellTemp &lt;span style="color: blue"&gt;As New &lt;/span&gt;StringBuilder
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;DataTemplate &amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns='http://schemas.microsoft.com/winfx/&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;2006/xaml/presentation' &amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' &amp;quot;&lt;/span&gt;)

&lt;span style="color: green"&gt;'Be sure to replace &amp;quot;YourNamespace&amp;quot; and &amp;quot;YourAssembly&amp;quot; with your app's 
'actual namespace and assembly here
&lt;/span&gt;CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:local = 'clr-namespace:YourNamespace&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;;assembly=YourAssembly'&amp;gt;&amp;quot;&lt;/span&gt;)

CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;Grid&amp;gt;&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;Grid.Resources&amp;gt;&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;local:DateTimeConverter x:Key='DateConverter' /&amp;gt;&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/Grid.Resources&amp;gt;&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;TextBlock &amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;Text = '{Binding Birthday, &amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;Converter={StaticResource DateConverter}}' &amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;Margin='4'/&amp;gt;&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/Grid&amp;gt;&amp;quot;&lt;/span&gt;)
CellTemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/DataTemplate&amp;gt;&amp;quot;&lt;/span&gt;)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;CellETemp &lt;span style="color: blue"&gt;As New &lt;/span&gt;StringBuilder
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;DataTemplate &amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns='http://schemas.microsoft.com/winfx/&amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;2006/xaml/presentation' &amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' &amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;xmlns:basics='clr-namespace:System.Windows.Controls;&amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;assembly=System.Windows.Controls' &amp;gt;&amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;basics:DatePicker &amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;SelectedDate='{Binding Birthday, Mode=TwoWay}' /&amp;gt;&amp;quot;&lt;/span&gt;)
CellETemp.Append(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/DataTemplate&amp;gt;&amp;quot;&lt;/span&gt;)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;TemplateColumn.CellTemplate = XamlReader.Load(CellTemp.ToString())
TemplateColumn.CellEditingTemplate = XamlReader.Load(CellETemp.ToString())
targetDataGrid.Columns.Add(TemplateColumn)&lt;/pre&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8395118" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/scmorris/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/DataGrid/default.aspx">DataGrid</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/Value+Converters/default.aspx">Value Converters</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/Template+Columns/default.aspx">Template Columns</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/Silverlight+2/default.aspx">Silverlight 2</category></item><item><title>Defining Columns for a Silverlight DataGrid</title><link>http://blogs.msdn.com/scmorris/archive/2008/03/27/defining-columns-for-a-silverlight-datagrid.aspx</link><pubDate>Fri, 28 Mar 2008 04:28:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8340724</guid><dc:creator>scmorris</dc:creator><slash:comments>43</slash:comments><comments>http://blogs.msdn.com/scmorris/comments/8340724.aspx</comments><wfw:commentRss>http://blogs.msdn.com/scmorris/commentrss.aspx?PostID=8340724</wfw:commentRss><description>&lt;table cellspacing="0" cellpadding="5" width="100%" bgcolor="#fff8c9" border="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top"&gt;This post has been updated to work with the &lt;a href="http://silverlight.net/GetStarted/" target="_blank"&gt;RTW version of the Silverlight 2 DataGrid&lt;/a&gt;.&amp;#160; The code examples are not guaranteed to work with previous Beta versions of the DataGrid. &lt;a href="http://blogs.msdn.com/scmorris/archive/2008/10/14/silverlight-2-datagrid-is-released.aspx" target="_blank"&gt;Read more about the features that the Silverlight 2 DataGrid has to offer...&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;If you read my &lt;a href="http://blogs.msdn.com/scmorris/archive/2008/03/21/using-the-silverlight-datagrid.aspx" target="_blank"&gt;last post&lt;/a&gt;, you might have noticed how easy it is to get a Silverlight DataGrid up and running with the AutoGenerateColumns feature.&amp;#160; Something else you might have noticed is that if you don't like the default column choices it is sort of hard to change them.&amp;#160; Not to fear however since there are two ways to get more control.&amp;#160; One way is to use the AutoGeneratingColumn event (we'll talk about this in a different post) but the easiest way, and the topic of our discussion today, is by using the Columns collection.&lt;/p&gt;  &lt;h4&gt;The Columns Collection and You&lt;/h4&gt;  &lt;p&gt;The Columns Collection is your ticket to controlling the DataGrid's column order, appearance, and even what controls they use to represent data in their cells.&amp;#160; It enables you to have a concrete instance for each column that you define unlike auto generation that simply fills the column collection for you.&lt;/p&gt;  &lt;p&gt;Before we dive into using the Columns collection though, it is useful to know what you can put in it.&lt;/p&gt;  &lt;h5&gt;DataGrid Column Types&lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;DataGridTextColumn&lt;/strong&gt; - When you think of a standard column in a DataGrid, you probably picture something that looks a lot like a DataGridTextColumn.&amp;#160; This column is the default and uses a TextBlock to display its data, and a TextBox to allow editing of its data.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;DataGridCheckBoxColumn &lt;/strong&gt;- The DataGridCheckBoxColumn is the first out of box custom column type.&amp;#160; It provides a read-only CheckBox for displaying a boolean or nullable boolean value, and a normal CheckBox to allow editing of that value.&amp;#160; This column derives from DataGridBoundColumn and serves as a sample for building your own column types.&amp;#160; We'll walk through the process of building your own column types in a future post.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;DataGridTemplateColumn &lt;/strong&gt;- If the DataGrid was a movie, the DataGridTemplateColumn would be the star.&amp;#160; It is the most versatile column but, like most prima donnas, takes the most work to get up and going.&amp;#160; Once it is going though there is pretty much no limit to what it can do. &lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Using the Columns Collection&lt;/h5&gt;  &lt;p&gt;First we need to get our project back to where we ended last time.&amp;#160; Go ahead and follow the steps 1 and 2 &lt;a href="http://blogs.msdn.com/scmorris/archive/2008/03/21/using-the-silverlight-datagrid.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;If you followed steps 1 &amp;amp; 2 from the previous post, your DataGrid should look something like this: &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dg&amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;(If yours looks different because you did step 3, don't worry since it won't affect this walk through. The one exception is that you need to change IsReadOnly back to False.)&lt;/p&gt;

&lt;p&gt;Also, in the code behind file for Page.xaml you should be setting the source to a List&amp;lt;Data&amp;gt; where Data is the custom business object you created.&lt;/p&gt;

&lt;p&gt;Now that we are all on the same page lets start playing with the Column collection.&lt;/p&gt;

&lt;p&gt;Since auto generation is no longer needed, go ahead and set AutoGenerateColumns to False.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dg&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;AutoGenerateColumns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;False&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now add the Columns collection, and add a Text column in it bound to the &amp;quot;FirstName&amp;quot; property.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dg&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;AutoGenerateColumns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;False&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;First Name&amp;quot; 
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;FirstName&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The code above is doing a few things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It creates the &lt;strong&gt;Columns&lt;/strong&gt; collection using the &amp;lt;data:DataGrid.Columns&amp;gt; tag. &lt;/li&gt;

  &lt;li&gt;Inside that collection it creates a new &lt;strong&gt;DataGridTextColumn&lt;/strong&gt; &lt;/li&gt;

  &lt;li&gt;The content that you see in the column header is set to &amp;quot;First Name&amp;quot; using the &lt;strong&gt;Header &lt;/strong&gt;property &lt;/li&gt;

  &lt;li&gt;The column is data bound to the FirstName property using the &lt;strong&gt;Binding &lt;/strong&gt;property.&amp;#160; Notice that since the DataContext of the DataGrid is its ItemsSource collection a Source does not need to be specified.&amp;#160; &lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Side Note:&lt;/em&gt; Some of you might be wondering why we use &lt;strong&gt;Binding&lt;/strong&gt; here instead of a string like DisplayMember&lt;strong&gt;Path&lt;/strong&gt;.&amp;#160; The thinking behind this decision was that a binding gives you a lot more control of what ends up being shown.&amp;#160; It allows the use of converters, and leaves the door open for the addition of validation, and formatters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you run this you should see something like this:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="304" alt="FirstColumn" src="http://blogs.msdn.com/blogfiles/scmorris/WindowsLiveWriter/SpecifyingColumnsforaSilverlightDataGrid_80AC/FirstColumn_e2d39670-f708-4954-9c6a-bdb09e737111.png" width="404" border="0" /&gt; &lt;/p&gt;

&lt;p&gt;In addition to the &lt;strong&gt;Header &lt;/strong&gt;and &lt;strong&gt;Binding &lt;/strong&gt;properties, you can also customize the column through properties such as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Column Properties:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;CanUserResize &lt;/strong&gt;- Determines if this column can be resized.&amp;#160; In regards to its relationship with the DataGrid property CanUserResizeColumns, false always wins. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;CanUserReorder &lt;/strong&gt;- Determines if this column can be reordered with other columns.&amp;#160; In regards to its relationship with the DataGrid property CanUserReorderColumns, false always wins. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;CanUserSort &lt;/strong&gt;- Determines if this column can be sorted.&amp;#160; In regards to its relationship with the DataGrid property CanUserSortColumns, false always wins. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;IsReadOnly &lt;/strong&gt;- Determines if this column can be edited.&amp;#160; In regards to its relationship with the DataGrid property IsReadOnly, true always wins. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Width &lt;/strong&gt;- Sets the width of the column.&amp;#160; Setting this value overrides the DataGrid ColumnWidth property. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;MinWidth &lt;/strong&gt;- Sets the minimum width for this column. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Visibility &lt;/strong&gt;- Hides or shows this column &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ElementStyle &lt;/strong&gt;- The property used to style the display element for a column.&amp;#160; (In the case of the TextBox Column the display element is the TextBlock) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;EditingElementStyle &lt;/strong&gt;- The property used to style the editing element for a column.&amp;#160; (In the case of the TextBox Column the display element is the TextBox) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;TextBox Column Specific Properties:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;FontFamily&lt;/strong&gt; - Sets the &lt;a href="http://msdn2.microsoft.com/library/system.windows.controls.control.fontfamily.aspx" target="_blank"&gt;FontFamily&lt;/a&gt; property on both the TextBlock and TextBox. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;FontSize&lt;/strong&gt; - Sets the &lt;a href="http://msdn2.microsoft.com/library/system.windows.controls.control.fontsize.aspx" target="_blank"&gt;FontSize&lt;/a&gt; property on both the TextBlock and TextBox. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;FontStyle &lt;/strong&gt;- Sets the &lt;a href="http://msdn2.microsoft.com/library/system.windows.controls.control.fontstyle.aspx" target="_blank"&gt;FontStyle&lt;/a&gt; property on both the TextBlock and TextBox. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;FontWeight &lt;/strong&gt;- Sets the &lt;a href="http://msdn2.microsoft.com/library/system.windows.controls.control.fontweight.aspx" target="_blank"&gt;FontWeight&lt;/a&gt; property on both the TextBlock and TextBox. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Foreground&lt;/strong&gt; - Sets the &lt;a href="http://msdn2.microsoft.com/library/system.windows.controls.control.foreground.aspx" target="_blank"&gt;Foreground&lt;/a&gt; property on both the TextBlock and TextBox. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CheckBox Column Specific Properties:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;IsThreeState&lt;/strong&gt; - Sets the &lt;a href="http://msdn2.microsoft.com/library/system.windows.controls.primitives.togglebutton.isthreestate.aspx" target="_blank"&gt;IsThreeState&lt;/a&gt; property on the CheckBoxes &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets now finish off the columns that we used to have by adding two more DataGridTextBoxColumns and a DataGridCheckBoxColumn bound to &amp;quot;LastName&amp;quot;, &amp;quot;Age&amp;quot;, and &amp;quot;Available&amp;quot;.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dg&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;AutoGenerateColumns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;False&amp;quot; &amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;First Name&amp;quot;
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;FirstName&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Last Name&amp;quot; 
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;LastName&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Age&amp;quot; &lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;50&amp;quot; &lt;/span&gt;
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Age&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridCheckBoxColumn&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Available&amp;quot; 
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Available&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now when you run the application you have the same output as the auto generation, except now you control the order, what the header says, and you can set properties.&amp;#160; For instance in the code above the width of the Age column is set to 50 instead of the usual Auto.&lt;/p&gt;

&lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="304" alt="AllColumns" src="http://blogs.msdn.com/blogfiles/scmorris/WindowsLiveWriter/SpecifyingColumnsforaSilverlightDataGrid_80AC/AllColumns_f8727851-cea9-429b-b2a3-bd975c10c776.png" width="404" border="0" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;a name="template"&gt;
  &lt;h4&gt;Unleashing the Template Column&lt;/h4&gt;
&lt;/a&gt;

&lt;p&gt;Now that we have recreated the auto generated DataGrid from before let's add some new functionality, specifically a column that can be bound to a DateTime.&amp;#160; To do this we can either create a new column type, or just use a template column.&amp;#160; Since we only plan on using this column once, let's use a template column.&lt;/p&gt;

&lt;p&gt;First let's add something for this column to bind to.&amp;#160; Going back to your &lt;strong&gt;Data&lt;/strong&gt; class, add the following property:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;Birthday{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Private &lt;/span&gt;_birthday &lt;span style="color: blue"&gt;As &lt;/span&gt;DateTime

&lt;span style="color: blue"&gt;Property &lt;/span&gt;Birthday() &lt;span style="color: blue"&gt;As &lt;/span&gt;DateTime
    &lt;span style="color: blue"&gt;Get
        Return &lt;/span&gt;_birthday
    &lt;span style="color: blue"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;value &lt;span style="color: blue"&gt;As &lt;/span&gt;DateTime)
        _birthday = value
    &lt;span style="color: blue"&gt;End Set
End Property&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Also we need to update the Page.xaml code behind to initialize the new property:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;Page()
{
    InitializeComponent();

    &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Data&lt;/span&gt;&amp;gt; source = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Data&lt;/span&gt;&amp;gt;();
    &lt;span style="color: blue"&gt;int &lt;/span&gt;itemsCount = 100;

    &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i = 0; i &amp;lt; itemsCount; i++)
    {
        source.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Data&lt;/span&gt;()
        {
            FirstName = &lt;span style="color: #a31515"&gt;&amp;quot;First&amp;quot;&lt;/span&gt;,
            LastName = &lt;span style="color: #a31515"&gt;&amp;quot;Last&amp;quot;&lt;/span&gt;,
            Age = i,
            Available = i % 2 == 0&lt;strong&gt;,&lt;/strong&gt;
            &lt;strong&gt;Birthday = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Today.AddYears(-i)&lt;/strong&gt;
        });
    }

    dg.ItemsSource = source;                
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Public Sub New&lt;/span&gt;()
    InitializeComponent()

    &lt;span style="color: blue"&gt;Dim &lt;/span&gt;Source &lt;span style="color: blue"&gt;As &lt;/span&gt;List(&lt;span style="color: blue"&gt;Of &lt;/span&gt;Data) = &lt;span style="color: blue"&gt;New &lt;/span&gt;List(&lt;span style="color: blue"&gt;Of &lt;/span&gt;Data)
    &lt;span style="color: blue"&gt;Dim &lt;/span&gt;ItemsCount &lt;span style="color: blue"&gt;As Integer &lt;/span&gt;= 100

    &lt;span style="color: blue"&gt;For &lt;/span&gt;index &lt;span style="color: blue"&gt;As Integer &lt;/span&gt;= 1 &lt;span style="color: blue"&gt;To &lt;/span&gt;ItemsCount
        Source.Add(&lt;span style="color: blue"&gt;New &lt;/span&gt;Data() &lt;span style="color: blue"&gt;With &lt;/span&gt;_
        { _
            .FirstName = &lt;span style="color: #a31515"&gt;&amp;quot;First&amp;quot;&lt;/span&gt;, _
            .LastName = &lt;span style="color: #a31515"&gt;&amp;quot;Last&amp;quot;&lt;/span&gt;, _
            .Age = index, _
            .Available = (index &lt;span style="color: blue"&gt;Mod &lt;/span&gt;2 = 0)&lt;strong&gt;, _&lt;/strong&gt;
            &lt;strong&gt;.Birthday = DateTime.Today.AddYears(-index) _&lt;/strong&gt;
        })
    &lt;span style="color: blue"&gt;Next

    &lt;/span&gt;dg.ItemsSource = Source
&lt;span style="color: blue"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now that the data is there we can bind to it.&amp;#160; Going back to the Page.xaml file add a DataGridTemplateColumn to the Columns collection right after the &amp;quot;Available&amp;quot; column.&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;basics&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;clr-namespace:System.Windows.Controls;&lt;br /&gt;assembly=System.Windows.Controls&amp;quot;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dg&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;AutoGenerateColumns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;False&amp;quot; &amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;First Name&amp;quot;
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;FirstName&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Last Name&amp;quot; 
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;LastName&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTextColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Age&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;50&amp;quot;
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Age&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridCheckBoxColumn &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Available&amp;quot; 
                &lt;/span&gt;&lt;span style="color: red"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Available&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; /&amp;gt;

        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;&lt;strong&gt;DataGridTemplateColumn&lt;/strong&gt; &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Birthday&amp;quot;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Birthday&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;4&amp;quot;/&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellEditingTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;span style="color: #a31515"&gt;basics&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DatePicker 
                        &lt;/span&gt;&lt;span style="color: red"&gt;SelectedDate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Birthday&lt;/span&gt;&lt;span style="color: blue"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;Mode&lt;/span&gt;&lt;span style="color: blue"&gt;=TwoWay}&amp;quot; /&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGridTemplateColumn.CellEditingTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;&lt;strong&gt;DataGridTemplateColumn&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid.Columns&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Warning:&lt;/em&gt;&lt;/strong&gt; Be sure to add the DatePicker from the ToolBox so the basics namespace and associated reference to System.Windows.Controls is added for you.&lt;/p&gt;

&lt;p&gt;What's going on in the code above:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;A &lt;strong&gt;DataGridTemplateColumn&lt;/strong&gt; is being added to the Columns collection with its header being set to &amp;quot;Birthday&amp;quot;. &lt;/li&gt;

  &lt;li&gt;The &lt;strong&gt;CellTemplate &lt;/strong&gt;property, which takes a &lt;strong&gt;DataTemplate,&lt;/strong&gt; is being used to define the UI that cells in the column will use to display the data. &lt;/li&gt;

  &lt;li&gt;In this instance, the content of that DataTemplate is a TextBlock that has its Text property bound to the &amp;quot;Birthday&amp;quot; property on the Data Item.&amp;#160; Once again notice that no binding source is necessary since the default DataContext of a Template column's contents is the data item that it will be representing. &lt;/li&gt;

  &lt;li&gt;In addition to the CellTemplate the DataGridTemplateColumn's &lt;strong&gt;CellEditingTemplate &lt;/strong&gt;property is also being set.&amp;#160; This property is also a DataTemplate and defines the UI that cells in the column will display during edit mode. &lt;/li&gt;

  &lt;li&gt;In this instance, the content of the CellEditingTemplate's DataTemplate is a DatePicker control that has its SelectedDate property also bound to the &amp;quot;Birthday&amp;quot; property. &lt;/li&gt;

  &lt;li&gt;Notice that its binding has the &lt;strong&gt;Mode&lt;/strong&gt; property set to &lt;strong&gt;TwoWay&lt;/strong&gt;.&amp;#160; This is crucial to make sure that any changes made to this property at runtime will be pushed back into the data source. Note that you do not need to set the Mode when you are using the DisplayMemberBinding property in column types other than DataGridTemplateColumn because the columns themselves take care of this. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you run this you should see a new column in your DataGrid that uses a TextBlock to display a date, and then switches to a DatePicker during edit mode to provide a richer input experience.&lt;/p&gt;

&lt;p&gt;(If your DataGrid still has all of the properties set from Step 3 of the last post you need to set &lt;strong&gt;IsReadOnly&lt;/strong&gt; back to &lt;strong&gt;False&lt;/strong&gt; if you want to be able to see the DatePicker in edit mode)&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="320" alt="TemplateColumn" src="http://blogs.msdn.com/blogfiles/scmorris/WindowsLiveWriter/SpecifyingColumnsforaSilverlightDataGrid_80AC/TemplateColumn_348a43ea-c3b0-42c5-b064-f12e29a5bac9.png" width="431" border="0" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h4&gt;Using Value Converters&lt;/h4&gt;

&lt;p&gt;This column is pretty much exactly what we want, but there is a finishing touch missing.&amp;#160; If you look at the text, it has an annoying 12:00:00 AM after each date.&amp;#160; Since we only care about the date itself and not the time, let's reformat this string to the short date format that matches what is shown in the DatePicker.&lt;/p&gt;

&lt;p&gt;Add a new class to your Silverlight project and name it &amp;quot;&lt;strong&gt;DateTimeConverter&lt;/strong&gt;&amp;quot;.&amp;#160; Then add:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Windows.Data;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Globalization;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeConverter&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IValueConverter
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public object &lt;/span&gt;Convert(&lt;span style="color: blue"&gt;object &lt;/span&gt;value, 
                       &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;targetType, 
                       &lt;span style="color: blue"&gt;object &lt;/span&gt;parameter, 
                       &lt;span style="color: #2b91af"&gt;CultureInfo &lt;/span&gt;culture)
    {
        &lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;date = (&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;)value;
        &lt;span style="color: blue"&gt;return &lt;/span&gt;date.ToShortDateString();
    }

    &lt;span style="color: blue"&gt;public object &lt;/span&gt;ConvertBack(&lt;span style="color: blue"&gt;object &lt;/span&gt;value, 
                              &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;targetType, 
                              &lt;span style="color: blue"&gt;object &lt;/span&gt;parameter, 
                              &lt;span style="color: #2b91af"&gt;CultureInfo &lt;/span&gt;culture)
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;strValue = value.ToString();
        &lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;resultDateTime;
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.TryParse(strValue, &lt;span style="color: blue"&gt;out &lt;/span&gt;resultDateTime))
        {
            &lt;span style="color: blue"&gt;return &lt;/span&gt;resultDateTime;
        }
        &lt;span style="color: blue"&gt;return &lt;/span&gt;value;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Imports &lt;/span&gt;System.Windows.Data

&lt;span style="color: blue"&gt;Public Class &lt;/span&gt;DateTimeConverter
    &lt;span style="color: blue"&gt;Implements &lt;/span&gt;IValueConverter

    &lt;span style="color: blue"&gt;Public Function &lt;/span&gt;Convert(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;value &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, _
                &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;targetType &lt;span style="color: blue"&gt;As &lt;/span&gt;System.Type, _
                &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;parameter &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, _
                &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;culture &lt;span style="color: blue"&gt;As &lt;/span&gt;System.Globalization.CultureInfo) _
                &lt;span style="color: blue"&gt;As Object &lt;/span&gt;_
                &lt;span style="color: blue"&gt;Implements &lt;/span&gt;System.Windows.Data.IValueConverter.Convert

        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;DateValue &lt;span style="color: blue"&gt;As &lt;/span&gt;DateTime = value
        &lt;span style="color: blue"&gt;Return &lt;/span&gt;DateValue.ToShortDateString()

    &lt;span style="color: blue"&gt;End Function

    Public Function &lt;/span&gt;ConvertBack(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;value &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, _
                &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;targetType &lt;span style="color: blue"&gt;As &lt;/span&gt;System.Type, _
                &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;parameter &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, _
                &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;culture &lt;span style="color: blue"&gt;As &lt;/span&gt;System.Globalization.CultureInfo) _
                &lt;span style="color: blue"&gt;As Object &lt;/span&gt;_
                &lt;span style="color: blue"&gt;Implements &lt;/span&gt;System.Windows.Data.IValueConverter.ConvertBack

        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;StrValue &lt;span style="color: blue"&gt;As String &lt;/span&gt;= value.ToString()
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;ResultDateTime &lt;span style="color: blue"&gt;As &lt;/span&gt;DateTime

        &lt;span style="color: blue"&gt;If &lt;/span&gt;DateTime.TryParse(StrValue, ResultDateTime) &lt;span style="color: blue"&gt;Then
            Return &lt;/span&gt;ResultDateTime
        &lt;span style="color: blue"&gt;End If

        Return &lt;/span&gt;value

    &lt;span style="color: blue"&gt;End Function
End Class&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you look at the code, a converter is a class that implements &lt;a href="http://msdn2.microsoft.com/library/system.windows.data.ivalueconverter.aspx" target="_blank"&gt;IValueConverter&lt;/a&gt; and as part of that provides two methods: Convert and ConvertBack.&amp;#160; These two methods are where you can perform your custom logic, in this case parsing a DateTime to and from a short date string format.&lt;/p&gt;

&lt;p&gt;Now that you have your reusable converter class you can use it in data binding.&lt;/p&gt;

&lt;p&gt;Back in your Page.xaml add a local xmlns.&amp;#160; To do this add the following in the UserControl tag next to the other xmlns declarations:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;clr-namespace:_____________&amp;quot;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Where the _________ is the name of your Application.&amp;#160; IntelliSense should provide the correct syntax as an option in the dropdown.&amp;#160; Next add the converter to your UserControl as a static resource.&lt;/p&gt;
&lt;span style="color: blue"&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;DateTimeConverter &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Key&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;DateConverter&amp;quot; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/span&gt;

&lt;p&gt;Finally use the converter in the TextBox's binding in the DataGridTemplateColumn.&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock 
    &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Birthday&lt;/span&gt;&lt;span style="color: blue"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;Converter&lt;/span&gt;&lt;span style="color: blue"&gt;={&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;DateConverter&lt;/span&gt;&lt;span style="color: blue"&gt;}}&amp;quot; &lt;/span&gt;&lt;span style="color: blue"&gt;
    &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;4&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now when you run the application, the text will be formatted in the manner that the converter specified.&lt;/p&gt;

&lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="304" alt="TemplateColumnWithConverter" src="http://blogs.msdn.com/blogfiles/scmorris/WindowsLiveWriter/SpecifyingColumnsforaSilverlightDataGrid_80AC/TemplateColumnWithConverter_6442a25f-0bd7-445a-8e17-92360ab1d62a.png" width="404" border="0" /&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8340724" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/scmorris/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/DataGrid/default.aspx">DataGrid</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/Value+Converters/default.aspx">Value Converters</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/Template+Columns/default.aspx">Template Columns</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/AutoGenerateColumns/default.aspx">AutoGenerateColumns</category><category domain="http://blogs.msdn.com/scmorris/archive/tags/Silverlight+2/default.aspx">Silverlight 2</category></item></channel></rss>