Hello,
When a new row is added to my grid (at the bottom), I always want to show that new added row. How can I do that?
Hello Markus,
In order to bring a DataItem or it's respective Record, you can use either the BringDataItemIntoView or the BringRecordIntoView method of the XamDataGrid.
1) When adding a new DataItem from code-behind:
private void Button_Click(object sender, RoutedEventArgs e){ Item newItem = new Item("NEW ITEM", 111, 222.0); (dataGrid.DataSource as ObservableCollection<Item>).Add(newItem); dataGrid.BringDataItemIntoView(newItem, false);}
dataGrid.BringDataItemIntoView(newItem, false);}
2) When adding a new Record from the UI by using the AddNewRecord:
private void dataGrid_RecordAdded(object sender, Infragistics.Windows.DataPresenter.Events.RecordAddedEventArgs e){ dataGrid.BringRecordIntoView(e.Record);}
I have attached a sample that uses the approaches from above.
If you have any questions, please let me know.
Thanks for the answer, but I use an ObservableCollection in a ViewModel to add a new record. In that viewModel I don't have access to the xamDataGrid control. Is there an alternative method without using the record object? e.g. XamDatGrid.ScrollToLast(). So I just need to implement an event when a new item is added to my ObservableCollection, without using the Record object.
In order to scroll to the last record whenever a new one is added from the ViewModel without interfering with the ViewModel at all, you can create a simple class that extends the XamDataGrid and handle the CollectionChanged event of the Records collection. This will keep the code-behind clean and all the logic within the extended class can remain View specific.
public class CustomXamDataGrid : XamDataGrid{ public CustomXamDataGrid() : base() { this.Loaded += CustomXamDataGrid_Loaded; } protected void CustomXamDataGrid_Loaded(object sender, System.Windows.RoutedEventArgs e) { this.Records.CollectionChanged += Records_CollectionChanged; } protected void Records_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { this.BringRecordIntoView(this.Records[this.Records.Count - 1]); } }}
protected void CustomXamDataGrid_Loaded(object sender, System.Windows.RoutedEventArgs e) { this.Records.CollectionChanged += Records_CollectionChanged; }
protected void Records_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { this.BringRecordIntoView(this.Records[this.Records.Count - 1]); } }}
If you are still experiencing any issues on the matter, feel free to modify the sample I have attached according to your functionality so the issue is reproduced and send it back to me for further investigation.