views:

20

answers:

1

How can the "Header" property of the DataGridTemplateColumn (or DataGridTextColumn) be bound to some text property of the ViewModel in the DataGrid of Silverlight 4?

"Ideal" solution (that does not work):

<sdk:DataGrid AutoGenerateColumns="False" ...>
  <sdk:DataGrid.Columns>
    <sdk:DataGridTemplateColumn Header="{Binding MyViewModelProperty}" >

I heard that the Header property is not a FrameworkElement and therefore cannot use DataBinding directly. Is the Header property still no FrameworkElement in SL 4? At least Blend 4 does not want to DataBind on the Header property.

Now is there a "good" workaround that does not required dirty hacks in the code behind file? Maybe using a class that is derived from DataGridTemplateColumn? How could that be done?

I'd prefer a solution that works entirely in XAML. What I've found on stackoverflow (and the web) so far did not solve my question (e.g. http://stackoverflow.com/questions/1312613/databinding-the-datagrid-column-header-in-code).

A: 

That is still true for SL4 - bindings must be to Framework elements. One option that you have is to use the AutoGenerateColumns="True" and the attributes in the System.ComponentModel.DataAnnotations namespace like DisplayAttribute to define the column names.

Michael S. Scherotter