0
votes

I have a DataGrid in a Silverlight control which contains custom controls. These custom controls are used outside the grid too and work perfectly. When used within the grid however, they appear to reset themselves when focus is lost (i.e. we move to another cell).

To test that it wasn't my custom control, I added a column containing an AutoComplete box. When typing into the box and tabbing to the next column the box is cleared.

When debugging I can see that the controls loaded event is called after we lost focus from it, which confirms that it's being reloaded/recreated when moving to the next field!? Is it simply not committing the change to the grid for some reason?

e.g. of XAML (very basic, nothing weird here)

        <sdk:DataGridTemplateColumn Header="Description">
             <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                            <sdk:AutoCompleteBox Name="Description" Width="100" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
1

1 Answers

1
votes

This is perfectly normal - the CellTemplate is used when the cell is rendered, and they get destroyed when the row is scrolled out of view if you have virtualisation turned on.

The CellEditingTemplate is used when the grid cell goes into editing mode, at this point your editing control will get created - IOW the editing control is created on demand. Just focussing on the cell is not sufficient to put the cell into edit mode by itself.