我最近使用 DataGrid 完成了这项工作。诀窍是将自定义行为附加到您的视图。像那样。
<ListView ... >
<iy:Interaction.Behaviors>
<ext:ScrollIntoViewBehavior />
</iy:Interaction.Behaviors>
...
</ListView>
和相关的类:
public class ScrollIntoViewBehavior : Behavior<ListView>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.SelectionChanged -= new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
}
private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ListView)
{
ListView grid = (sender as ListView);
if (grid.SelectedItem != null)
{
grid.Dispatcher.BeginInvoke(() =>
{
grid.UpdateLayout();
grid.ScrollIntoView(grid.SelectedItem);
});
}
}
}
}
需要注意的一些事项: