public class CustomBehavior : Behavior<SfDataGrid>
{
protected override void OnAttached()
{
this.AssociatedObject.Loaded += AssociatedObject_Loaded;
this.AssociatedObject.CurrentCellActivating += AssociatedObject_CurrentCellActivating;
}
private void AssociatedObject_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs e)
{
var rowIndex = e.CurrentRowColumnIndex.RowIndex;
var dataRow = AssociatedObject.RowGenerator.Items.FirstOrDefault(row => row.RowIndex == rowIndex) as DataRow;
//you can write your own code here.
}
private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
this.AssociatedObject.View.Records.CollectionChanged += Records_CollectionChanged;
}
private void Records_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
var rowIndex =this.AssociatedObject.ResolveToRowIndex(e.NewItems[0]);
this.AssociatedObject.ScrollInView(new RowColumnIndex(rowIndex,this.AssociatedObject.GetFirstColumnIndex()));
AssociatedObject.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(() =>
{
var dataRow = AssociatedObject.RowGenerator.Items.FirstOrDefault(row => row.RowIndex == rowIndex) as DataRow;
//you can write your own code here.
}));
}
}
} |