Created
November 29, 2012 01:32
A simple interactivity behavior to keep the a DataGird's SelectedItem into view
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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment