Chris W.
Chris W.

Reputation: 23280

DataGrid CellTemplate TabNavigation

I assume this has been asked before but I couldn't find it, so let me know if it's a duplicate found with other verbiage or something.

The problem is with an SL4 DataGrid which contains multiple CellTemplate's including Checkbox, Button etc. By default it will only tab through these elements on the first row. If I set TabNavigation="Cycle" it will tab through all the elements, but it doesn't move on to the next elements and instead just re-iterates the tabbing through the same DataGrid.

If I set it to Once then again it will only tab through the first row....and SL4 doesn't appear to have a Continue option to move onto the next object once it reaches the edge.

I'm looking for just an easy way to take the equivalent of TabNavigation="Cycle" except when it reaches the last tab-able element in the DataGrid then it moves on to the next thing in the tree instead of just tabbing back to the first element in the DataGrid again. What am I missing here?

Upvotes: 2

Views: 910

Answers (2)

Savaratkar
Savaratkar

Reputation: 2084

I had some exp wth SL4 long time back. I will give your problem a try:

See the property you set to get your desired behavior won't work. It will be the Microsoft way only, thus alternative is to write your own code to achieve the required behavior.

My idea is to attach the following event to each datagrid cell:

private void DataGridCell_KeyDown(object sender, KeyEventArgs e)
 {

   if (keypressed == 'TAB' && last cell of the datagrid)
    {
          e.handled=true;
          int tabIndex = dg.TabIndex;
           tabindex++;
           Control control = GetControl(tabIndex); // You can use visual tree in the method to get it
           control.select();
           control.focus();
   }
 }

My apologies I have written pseudo instead of real code, since it will take time for me to recall the code I use to do in SL.

Hope this solution works for you both way, when you tab out of data-grid and reverse tab in to the data-grid.

Upvotes: 0

Rob J
Rob J

Reputation: 6629

There doesn't seem to be a native way to do this in Silverlight, here is a list of supported key strokes in the data grid control: http://msdn.microsoft.com/en-us/library/cc838112(v=vs.95).aspx

I was able to fake it by using a KeyDown event and checking for Tab then setting the editing cell manually:

<Grid x:Name="LayoutRoot"  >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" Text="Some text" />
    <sdk:DataGrid Grid.Row="1" ItemsSource="{Binding People}" AutoGenerateColumns="False" KeyDown="DataGrid_KeyDown">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding FirstName}" />
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellEditingTemplate>
            </sdk:DataGridTemplateColumn>
            <sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding LastName}" />
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellEditingTemplate>
            </sdk:DataGridTemplateColumn>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>
    <TextBox Grid.Row="2" Text="Some more text" />
</Grid>

    private void DataGrid_KeyDown(object sender, KeyEventArgs e)
    {
        DataGrid dg = (DataGrid)sender;
        ObservableCollection<Person> items = dg.ItemsSource as ObservableCollection<Person>;
        if (e.Key == Key.Tab && dg.SelectedIndex < items.Count -1)
        {
            dg.SelectedIndex++;
            dg.CurrentColumn = dg.Columns[0];
            dg.BeginEdit();
            var cell = dg.CurrentColumn.GetCellContent(dg.SelectedItem);
        }
    }

Upvotes: 1

Related Questions