DataGrid columns using DataTemplate

Sep 16, 2009 at 11:33 AM

Is there a way to define datagrid columns using DataTemplate?

Coordinator
Sep 18, 2009 at 8:49 PM

Hi bnovicewpf,

I don't believe that there's a way to do this.  What is your scenario, maybe there's another way to accomplish it?

Thanks,

Saied K.

Sep 18, 2009 at 8:56 PM

The way it works with ListView is that we can set ItemsTemplate with the DataTemplate which contains Grid and then column information.

Same doesn't work with DataGrid though.

The idea is to intialize the datagrid without knowing it is a datagrid (from seperate module)

Oct 15, 2009 at 9:55 AM

The idea was to make datagrid control as dumb as possible. Adhering to MVP (Model-View-Presenter) I wanted the presenter to specify the column definition for the view. Another objective was to allow user to bind same Model data to different views. Thus the way I achieved for other collection controls ListBox, Listview, etc was to specify the control definition as DataTemplate and set it to ItemTemplate f the control. 

But doing the same with WPF datagrid was not working, thus I created a custom DataGrid control inheriting from WPF DataGrid and hide "Columns" (type: ObservableCollection<DataGridColumn>) by creating dependency property of the same. Now the presenter specifies the DataGridColumn collection and binds to "Columns" property of the custom control (view). The view then manually add's the columns to DataGrid control.

I am very eager to know whether the approach I have followed is correct or not.

Is there another better/nice/optimum way to achieve the above?