This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows.Controls; | |
using System.Windows.Interactivity; | |
public class KeepSelectionInView : Behavior<DataGrid> | |
{ | |
protected override void OnAttached() | |
{ | |
base.OnAttached(); | |
this.AssociatedObject.SelectionChanged += DataGrid_SelectionChanged; | |
} | |
protected override void OnDetaching() | |
{ | |
base.OnDetaching(); | |
this.AssociatedObject.SelectionChanged -= DataGrid_SelectionChanged; | |
} | |
void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) | |
{ | |
if (AssociatedObject.SelectedItem != null) | |
AssociatedObject.ScrollIntoView(AssociatedObject.SelectedItem); | |
else if (AssociatedObject.SelectedItems != null && AssociatedObject.SelectedItems.Count > 0) | |
AssociatedObject.ScrollIntoView(AssociatedObject.SelectedItems[0]); | |
} | |
} |
No comments:
Post a Comment