CellTemplate while binding to ItemsSource?

4 posts, 0 answers
  1. Buzz
    Buzz avatar
    32 posts
    Member since:
    Oct 2010

    Posted 30 Nov 2010 Link to this post

    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.

  2. Vlad
    Vlad avatar
    11100 posts

    Posted 01 Dec 2010 Link to this post


     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,
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
  3. Buzz
    Buzz avatar
    32 posts
    Member since:
    Oct 2010

    Posted 02 Dec 2010 Link to this post

    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.


  4. Maya
    Maya avatar
    4062 posts

    Posted 07 Dec 2010 Link to this post

    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,
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
Back to Top