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

using data row with cell template selector

3 Answers 219 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lou
Top achievements
Rank 1
Lou asked on 09 Dec 2010, 10:51 PM

I need to apply a data template to a grid view cell using a cell template selector similar to the approach provided in one of the grid view demo's. My template looks like this:

 

 

 

 

<DataTemplate>

 

 

 

 

<TextBlock Text="{Binding Converter={StaticResource dataRowConverter}}" Foreground="Gray" />

 

 

 

 

</DataTemplate>

The data grid is bound to a data table, so I use the dataRowConverter to try to get the right column value out of the data row that is passed to it by this template. My problem is that in the converter, I do not have the index to the data row to get the corresponding value for the grid view cell being processed. How am I able to pass as a converter parameter the current cell header or name to the converter? Thanks for your help.

 

3 Answers, 1 is accepted

Sort by
0
Lou
Top achievements
Rank 1
answered on 10 Dec 2010, 05:35 PM
Ok, I was able to solve my problem by coding a new data template instance in the template selector class. At that point, the data row and the cell aquiring the template is passed to the template selector, so I can create a custom template and return it and also set the value of the cell at that point and I ended up not needed the converter, though my in my first experiment I found that I could easily add a converter and the correct converter parameter at that point as well. Below is a code snippet from the template selector class:

 

 

DataRow row = (DataRow) item;
List<string> greyedOutColumns = (List<string>)row["greyedOutColumns"];
FrameworkElementFactory newFactory = new FrameworkElementFactory(typeof(TextBlock));

 

 

 

/* to use a converter:

 

Binding binding = new Binding();
binding.Converter = MyConverterClass();
binding.ConverterParameter = cell.Column.Header.ToString();
newFactory.SetBinding(TextBlock.TextProperty, binding);

*/

 

 

 

 

newFactory.SetValue(

 

TextBlock.ForegroundProperty, Brushes.BlueViolet);
newFactory.SetValue(TextBlock.TextProperty, row[cell.Column.Header.ToString()].ToString());
DataTemplate cellTemplate = new DataTemplate();
cellTemplate.VisualTree = newFactory;
cellTemplate.Seal();

 

 

 

return cellTemplate;

 

0
Vanya Pavlova
Telerik team
answered on 10 Dec 2010, 06:02 PM
Hi Lou,

Can we consider this thread closed?

All the best,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Lou
Top achievements
Rank 1
answered on 10 Dec 2010, 06:14 PM
Yes you can. Thanks.
Tags
GridView
Asked by
Lou
Top achievements
Rank 1
Answers by
Lou
Top achievements
Rank 1
Vanya Pavlova
Telerik team
Share this question
or