How do I change DataTemplate for Seleccted Row

Jul 26, 2009 at 8:58 AM

In my DataGrid, I need to Display the CellEditingTemplate DataTemplate for all cells in the selected Row (unless the datagrid is readonly), and the CellTemplate for all other rows.

I have tried using a TemplateSelector like this...

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            //if (item == null)
            //    return base.SelectTemplate(item, container);

            ContentPresenter presenter = container as ContentPresenter;
            DataGridCell cell = presenter.Parent as DataGridCell;

            if (cell.IsSelected)
                return CellEditingTemplate;
            else
                return CellTemplate;

The above selector changes the template when the row is selected, but not when it is unselected.

 

How can I acheive this?