Setting Focus On DataGrid

Aug 29, 2008 at 3:08 PM
I want to set focus on the first cell of the DataGrid when the window loads. I'm trying this...

personnelDataGrid.Focus();
personnelDataGrid.SelectedIndex = 0;

The selected index statement is working nicely and placing a highlight on the first row of the grid but I can't figure out why the user can't immediately arrow down the list using the keyboard.  The user has to either press the tab key or use the mouse to get focus on the top row (or any cell of the top row). I guess it's because the grid has focus but the first cell does not. How do I get the grid to focus on the first cell of the top row when the window loads?

Thanks,
Lee



Coordinator
Aug 29, 2008 at 7:33 PM
Try something like

DataGridCell cell = GetCell(0,0);
cell.Focus();

when the window is loaded.  The implementation for this is on this thread, http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=34065.
Aug 29, 2008 at 8:03 PM
When I type DataGridCell it is not in scope. Do I need a using statement at the top of my C#?
Coordinator
Aug 29, 2008 at 8:59 PM
DataGridCell has the same scope as DataGrid.  I'm not sure why you cannot find it.  The using statement should be:

using

 

Microsoft.Windows.Controls;

 

Aug 29, 2008 at 10:00 PM

Adding that reference helped, thanks.

When I try your example from the other thread I get an error from this line
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

The error I get is...

 

 

"The non-generic method 'System.Windows.FrameworkElement.GetVisualChild(int)' cannot be used with type arguments"

 

Coordinator
Aug 29, 2008 at 10:42 PM
GetVisualChild<T>() is actually a method that you are suppose to implement similar to this:

public

 

static T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child =
default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v
as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
         }
    }
    return child;
}

 

Aug 31, 2008 at 7:47 PM

Ok thanks. This compiles now but when I step through the GetRow module on this line ...

personnelDataGrid.ScrollIntoView(personnelDataGrid.Items[index]);

... I get the following error...


"Operation is not valid while ItemCollection has no inner collection. ItemCollection is uninitialized or binding on ItemsControl.ItemSource supplied null for collection."

Coordinator
Sep 1, 2008 at 5:38 PM
Have you set the personnelDataGrid.ItemsSource already before calling this code?
Oct 24, 2008 at 1:07 PM
Hi

I do the following, BEUserGroup is my business object, the grid is already containing some rows, and I want to focus on a cell of a freshly added new item/row, which is added as first row by thre following code:

      Dim oItem As New BE.BEUserGroup
      oItem.UserGrpName = "<Name>"
      oItem.Desc = "<Desc>"
      Call mltData.Add(oItem)

Now I want to focus on a cell in this new row

      Dim oCell As DataGridCell = Helpers.GetCell(dgData, 0, 1)
      oCell.Focus()

This dows not work, because GetCell, the presenter is nothing (rowcontainer has no visual children)
         Dim presenter As DataGridCellsPresenter = GetVisualChild(Of DataGridCellsPresenter)(rowContainer)

Is there a workaround for this?
Regards
Klaus



Coordinator
Oct 30, 2008 at 1:09 PM
Have you tried a DataGrid.UpdateLayout() and DataGrid.ScrollIntoView()?
Oct 30, 2008 at 2:37 PM
Vinsibal

DataGrid.UpdateLayout allows the focus move, thanks for that.

But is there a way to set the focused cell into edit mode?
After the move focus I can start entering data, but the cursor is not blinking, so the user has no visual clue that he can start entering data.

Regards
Klaus


Coordinator
Oct 31, 2008 at 2:19 PM
After the focus, try calling DataGrid.BeginEdit().
Oct 31, 2008 at 2:32 PM

Vinsibal

Thanks for the link.

I already found this, but it really does not help me

Can you hint me in some documentation for
- ValidationStep
- RawProposed or ConvertedProposed
So that I have an alternative? I really can not find anything on this!

Regards
Klaus

Coordinator
Oct 31, 2008 at 4:11 PM
Sorry, I'm a little confused about your question.  You were asking previously, "But is there a way to set the focused cell into edit mode?".  Setting BeginEdit will do that.  What exactly does the ValidationStep have to do with beginning the edit?
Oct 31, 2008 at 4:18 PM

Vinsibal

I answered to the wrong mail, I will send the response again to you regarding V1 validation

Regarding SetFocus: BeginEdit does the job! Thank for your support

Best regards
Klaus