Home Forums WPF controls Xceed DataGrid for WPF How to set CurrentColumn, CurrentItem?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26441 |

    By default, DataGridControl won’t take keyboard focus (e.g. call Focus() on it and nothing happens). From an earlier post on this forum, I’m given to understand that to get this to work you have to set CurrentColumn and CurrentItem to the starting column and item first.

    Given:
    var view = Data.Tables[table.DbName].DefaultView;
    Grid.DataContext = new DataGridCollectionView(view);

    I then tried:
    Grid.CurrentColumn = Grid.Columns[0];

    But that failed because Grid.Columns is an empty list, even though for display purposes it did in fact pick up the columns and is happily displaying them. What am I doing wrong?

    The same thing applies to Grid.Items; there’s also apparently the problem that with virtualization, you can’t be sure any given item will actually exist at any given moment. How do you set CurrentItem? (That’s something that’ll be needed for other purposes such as implementing cursor keys, so the question will arise in general.)

    Imported from legacy forums. Posted by Russell (had 2089 views)

    User (Old forums)
    Member
    Post count: 23064

    I found the solution to the first problem: it turns out that when you set Grid.DataContext, Columns and Items aren’t updated immediately. What you need to do is hook the Loaded event, at which time these things will be set, then you can access Columns[0] and Items[0], thus:

    void Grid_Loaded(object sender, RoutedEventArgs e)
    {
    Grid.CurrentColumn = Grid.Columns[0];
    Grid.CurrentItem = Grid.Items[0];
    Grid.Focus();
    }

    I’d still be interested if anyone knows what the situation is with using Items in the presence of virtualization?

    Imported from legacy forums. Posted by Russell (had 3297 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.