This is a migrated thread and some comments may be shown as answers.

CellTemplate while binding to ItemsSource?

3 Answers 181 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Buzz
Top achievements
Rank 1
Buzz asked on 30 Nov 2010, 09:35 PM
I am binding my RadGridView's ItemsSource to a System.Data.DataSet.  This dataset is readonly and is not interpreted by my application...  it's just obtained and displayed for the user to view.  It comes from a web service and the idea is that this web service may change the contents being served up in the future.  The number of columns in the dataset may change down the road.

There is one important exception to this.  Once my application obtains the dataset from the web service, it adds a column onto it and populates it with objects of a complex type.
In my gridview, I'd like to display these objects with much more than just the default ToString.
I'd like to use a DataTemplate to do this.

I see the GridViewDataColumn's CellTemplate.  But I'm not defining GridViewDataColumns in my RadGridView, because, as I mentioned, I'm binding the GridView's ItemsSource to my DataSet.  So, at least at first glance, that doesn't seem useful to me.

In addition, I'm using the MVVM application model.  So although I'm tempted to drop the binding to the ItemSource, manually add a GridViewDataColumn for each of my columns, and set the CellTemplate on my special column, I'm resisting the urge.  Ideally I can do this all with binding.

How can I apply a DataTemplate to my cells while still binding to ItemsSource?

I wish I could specify a CellTemplateSelector for the entire GridView.  Then I'd just apply a special template when the cell contained an object of my type.

Thanks for any help.
Buzz



3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 01 Dec 2010, 08:15 AM
Hello,

 I'm not sure what exactly is not MVVM when adding columns programmatically? Can you clarify? You can define implicit style for GridViewCell type to style all cells at once. 

All the best,
Vlad
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Buzz
Top achievements
Rank 1
answered on 02 Dec 2010, 02:23 PM
You're right.  I'm new to MVVM and got confused there.  Binding is key to MVVM and I can still bind my view and viewmodel and programmatically add columns.
I did this and set the CellTemplate for my special column and it works for me.

I saw your blog post about subclassing GridViewDataColumn to create a custom column.
Is that the preferred way to do this, versus my use of a CellTemplate?
I tried it but used a TextBlock as my FrameworkElement.  I set the foreground to Purple and fontweight to Bold.  But when I ran it I only saw the Bold... no Purple.  Why would that be?

Programmatically setting the CellTemplate displayed the desired foreground color as expected, so for now I'm running with that.

Thanks
Buzz


0
Maya
Telerik team
answered on 07 Dec 2010, 06:10 PM
Hi Buzz,

The more appropriate approach depends on the custom settings and requirements. In case you want to create more specific columns with some particular CellEditTemplates and CellTemplates (just as it is in the blog post you are referring to), it is better to create a custom column class. Another proper scenario for this way to go is when you need to add a bit more equal columns with defined templates and it is much easier to define the custom column and refer it in xaml later. Furthermore, this technique may lead to a better performance, but again it all depends on the exact scenario.
However, as your case does not require any complicated controls to be inserted in the column's template, you may easily define the CellEditTemplate and CellTemplate of the GridViewDataColumn for example.
 

Kind regards,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
GridView
Asked by
Buzz
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Buzz
Top achievements
Rank 1
Maya
Telerik team
Share this question
or