Datagrid: collection of columns in a data template?

Mar 10, 2009 at 6:32 PM
Edited Mar 13, 2009 at 9:45 AM
hi =)
Is possible have a collection of columns in a DataTemplate ( or something like it ) and bind it in code to a DataGrid?

Today I have 8 Pages where the unique difference is the columns of  the DataGrid and it DataContext.

My idea is have only 1 page with a different column template collection for each DataContext and set it at runtime

like ...
<DataTemplate x:Key="dt1">  ' ???
<tk:DataGrid.Columns> '???
<tk:DataGridTextColumn Header="Country Name" Binding="{Binding Path=Country}"/> '???
<DataTemplate x:Key="dt2">
<tk:DataGridTextColumn Header="Fruit Name" Binding="{Binding Path=Fruit}"/>
<tk:DataGrid Name="dg1" ItemsSource="{Binding}">

Sub Bind(ByVal Source as String)
Select Case Source
Case "aaaa"
dg1.DataContext= myDataSet1.MyDataTable1
dg1.Columns = dt1 '??????
Case "bbbb"
dg1.DataContext= myDataSet2.MyDataTable1
dg1.Columns = dt2 '??????
End Select
End Sub

I write the code here, so dumb mistakes are possible :)

Any Idea? like this or something totally different but solve the question?
tks :)