views:

285

answers:

1

Hi,

I'm using DataTemplateSelector with the WPFToolkit DataGrid. I want to select the editing template for one cell based on the value of another cell on the same row.

The DataTemplateSelector's SelectTemplate method takes two arguments: one is the data item displayed by the grid row, the other is the grid cell.

What I want to know is how to get the value of another cell from within the SelectTemplate method. However, I'm not sure of the correct way to get this information by accessing properties of the cell.

    public class RangeValuesEditTemplateSelector : DataTemplateSelector
{
    public DataTemplate NumberTemplate{get; set;}
    public DataTemplate TextTemplate{get; set;}

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        //TODO: need to find the correct way to populate the if condition below
        DataGridCell theCell = container as DataGridCell;


        if (theCell.something.somethingElse)
        {
            return NumberTemplate;
        }else{
            return TextTemplate;
        }

    }


}

Can anyone help?

Many thanks in advance.

AT

A: 

I think you need an ItemViewModel. When you create these objects (one for each row); The itemViewModel should have additional properties that are set (as per your custom computation) on creation or via setters. so if EachRow has a property called "MoreImportantThanNextOne", you can set it in the ItemVM ctor, which takes in some data for the next row.
Then in the TemplateSelector override, you can just access the "MoreImportantThanNextOne" property values of the ItemVM to select the right template.

Gishu