Move to a new entry in the collection

I'm just getting started with Collections (ObservableCollections) and I hit a wall that I assumed would be easy. I'm sure this is easy, but I just can't find the answer.

I have a WPF screen with DataGrid on the left and TextBoxes on the right of the screen. The DataGrid is bound to an ObservableCollection (Activities) and I can click up and down the DataGrid and see the updated TextBoxes update with the correct information. Then I can change the information in the TextBoxes and save it back to the DB. Everything works perfectly!

However, when it comes to adding an entry to a collection, I am at a loss as to the correct approach. I am using the Add method as shown below, but how do I navigate to this newly created post so that it can be edited? I've tried a dozen approaches, but I have yet to find the right approach. The text boxes just stay focused on the last edited record. Any ideas?

Private Activities As ObservableCollection(Of ActivityRecord)

Private Sub AddMode()
    Dim _ActivityRecord As New ActivityRecord(0, DateTime.Now, Nothing, "", gWorkerID, "")
    Activities.Add(_ActivityRecord)
'Code to move to the newly created record should go here

      

0


a source to share


1 answer


In WPF, each ItemsControl (DataGrid) secretly uses a derived CollectionView to facilitate navigation between records / items - in fact it is a class that provides a currency mechanism that helps other controls (such as a text box) determine which data bound item is current ...

If you create a new object and add it to your ObservableCollection, you can use one of CollectionView's MoveXXX move methods to make that item live. You can (depending on what type of CollectionView you get) also call the Add method on the CollectionView and it will automatically call the Add method on your base ObservableCollection.



Either way, getting a reference to the CollectionView is a secret. You can use the static CollectionView method (I forgot its name) to get the view used for your DataGrid, or (and this is my preferred method), you can explicitly create a ListCollectionView and bind your DataGrid to it, not an ObservableCollection.

0


a source







All Articles