Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Cell focus in a data grid
Message
De
07/01/2012 03:54:20
 
 
À
06/01/2012 11:53:04
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 4.0
Application:
Desktop
Divers
Thread ID:
01532185
Message ID:
01532367
Vues:
17
>>>I have a data grid bound to any number of rows, most of the time just one, but when the grid receives focus, I really need the first cell of the first row to recieve editable focus. Any ideas how to do this?
>>
>>Something like this in the GotFocus() might work:
DG.CurrentCell = new DataGridCellInfo(DG.Items[0], DG.Columns[0]);
>>DG.UnselectAllCells();
>>DG.SelectedCells.Add(DG.CurrentCell);
>>DG.BeginEdit();
But I've a feeling the user would find it a bit disconcerting. The OnFocus only fires when they click on an actual cell and that may be the cell they actually want to edit? Also if there's a scrollbar and the user uses it then the OnFocus fires when they move back to the Grid
>
>Good points Viv, I suspect I don't want the focus moved around every time that event fires. Maybe only when the expander gets focus, a tab could move to the first cell of the first row. After that, the tab inside the grid would work normally.

What would you want to happen when an Exander gets focus but is not expanded? Given a DataGrid in a collapsed Expander (XP2) this would expand it and set the first cell in editmode but if already expanded then focus would be set to the last focused element:
private void XP2_GotFocus(object sender, RoutedEventArgs e)
        {
            if (!XP2.IsExpanded)
            {
                XP2.IsExpanded = true;
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    DG.SelectionUnit = DataGridSelectionUnit.Cell;
                    DG.CurrentCell = new DataGridCellInfo(DG.Items[0], DG.Columns[0]);
                    DG.UnselectAllCells();
                    DG.SelectedCells.Add(DG.CurrentCell);
                    DG.BeginEdit();
                }
            ));

            }
            else
            {
                var v = FocusManager.GetFocusedElement(DG);
                if (v != e.OriginalSource)
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        v.Focus();
                     }));
            }
        }
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform