1

This is an annoying bug I have to deal with since the beginning on my DataGrid work and I can't find a solution without it to dismantle something else.

I've a DataGridTemplateColumn in my DataGrid, which contains a ComboBox, and some other cells before and after (just some text). When the user leaves the last cell's edit, it does it by pressing the Tab key, in order to move the focus on the next cell, my DataGridTemplateColumn. The problem I had is that it first focused the cell, and then the ComboBox inside by pressing Tab once again.

So I've used the datagrid_previousKeyDown (etc...) events, to write something like that :

GridContentMethods.GetCell(this.gridCameras, GridContentMethods.GetRow(
    this.gridCameras, this.gridCameras.SelectedIndex), 5).IsTabStop = false;
GridContentMethods.GetCell(this.gridCameras, GridContentMethods.GetRow(
    this.gridCameras, this.gridCameras.SelectedIndex), 5).Focusable = false;

If I do so, the grid loses the focus, but only if I was editing the previous cell. If I only navigate through my columns, it works perfectly.

Then, I was testing the code on this post. If I edit the previous cell, the focus is on the ComboBox, but I can't change the values with arrows as before, and above all, it doesn't keep the value displayed as soon as I leave the cell. And if I wasn't editing the previous cell, I have to press Tab twice to focus.

I also found out that my ComboBox wasn't displayed anymore but I only added an empty (and then the same) ComboBox in the CellTemplate above the CellEditingTemplate. It's here, but it doesn't work better. Here's my Xaml code for the first test above :

<DataGridTemplateColumn Header="FPS" Width="80" x:Name="Fps">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox x:Name="cbFps"
                      Foreground="Black"
                      HorizontalContentAlignment="Left"
                      DropDownClosed="ComboBox_DropDownClosed"
                      DropDownOpened="cbFps_DropDownOpened"
                      Initialized="cbFps_Initialized"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Sometimes I find a solution, but it's only to see that it didn't keep a previous working feature, and it drives me crazy.

How I would like it to work :

If the user moves the focus with the Tab key, from the text cell to the DataGridTemplateColumn, the focus has to be on the ComboBox inside, no matter if he was editing the previous cell or not. Plus, I want the ComboBox to keep the selected value, eventually selected with arrows, as it was before (I shouldn't add any more code about it).

Thanks for your help ! I think I'll be done with the DataGrid control after that.

Community
  • 1
  • 1
Adrana
  • 79
  • 11

0 Answers0