▶ 선택되어진 SelectedItem의 위치로 스크롤 이동시키기 Behavior 입니다.
public class ListBoxScrollIntoViewBehavior : Behavior<ListBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
base.OnDetaching();
}
private static void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
if (listBox?.SelectedItem == null)
return;
Action action = () =>
{
listBox.UpdateLayout();
// 선택된 아이템으로 스크롤 이동시키기.
if (listBox.SelectedItem != null)
listBox.ScrollIntoView(listBox.SelectedItem);
};
listBox.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
}
}
▶ Xaml에서 사용
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListBox ItemsSource="{Binding TestItemSource}" SelectionMode="Extended">
<i:Interaction.Behaviors>
<behavior:ListBoxScrollIntoViewBehavior />
</i:Interaction.Behaviors>
...
</ListBox>
</ScrollViewer>